Overriding Methods

D

danieleloff

The curious thing about Java is when I override a method in a derived
class, the overridden method is called in the base class. I'm hoping
this is somehow my mistake, as I'm new to Java and I cannot imagine why
anyone would do this, but it seems like the behaviour I'm witnessing.

That's a misfeature in my mind, because the base class cannot expect to
use methods from a derived class (especially before said class has been
constructed) and have them function as expected.

My dilema is this:

I called an accessor method in the constructor of my base class to set
a value. I created a derived class that overrides this method to add an
upper-bounds check. When I call the base class constructor using
super() the upper bound member is not yet initialized, and is the
default 0. The base class then calls the overridden method to set the
value, the upper bound is zero and the value is clipped to 0 regardless
of what it was. This is obviously not the desired behaviour!

Is my interpretation of what's happening correct? If so what's the
'Java' way of dealing with it?

Thanks,
-Dan
 
A

Adam Maass

The curious thing about Java is when I override a method in a derived
class, the overridden method is called in the base class.

This is correct.
That's a misfeature in my mind, because the base class cannot expect to
use methods from a derived class (especially before said class has been
constructed) and have them function as expected.

Why not? If the derived class specializes the behavior of a method, then
this exactly what you would expect.
My dilema is this:

I called an accessor method in the constructor of my base class to set
a value. I created a derived class that overrides this method to add an
upper-bounds check. When I call the base class constructor using
super() the upper bound member is not yet initialized, and is the
default 0. The base class then calls the overridden method to set the
value, the upper bound is zero and the value is clipped to 0 regardless
of what it was. This is obviously not the desired behaviour!

This is one of the "gotchas" in Java programming. You don't want to call
non-private, non-private instance methods from the constructor of the base
class for the very reason that these methods are always virtual, and you
could end up executing code in the derived class before the derived class'
constructor has finished executing.
Is my interpretation of what's happening correct? If so what's the
'Java' way of dealing with it?

In this case, maybe you want to promote bounds-checking to the superclass.

Or: You call another constructor in the base class that does not call the
overridden method; once the bound has been set in your derived class, you
call the method from that constructor.


-- Adam Maass
 
C

Chris Smith

The curious thing about Java is when I override a method in a derived
class, the overridden method is called in the base class. I'm hoping
this is somehow my mistake, as I'm new to Java and I cannot imagine why
anyone would do this, but it seems like the behaviour I'm witnessing.

That's a misfeature in my mind

I'd suggest that your mind is the one in error here, since what you're
complaining about one of the most fundamental aspects of object-oriented
programming. It's called polymorphism, and without it you aren't
writing OO software.
I called an accessor method in the constructor of my base class to set
a value.

Yep. You should be very careful about calling methods from a
constructor, unless they are private, final, or static. By "very
careful" I mean don't ever do it, unless you're willing to write a very
long essay on why that's what you want.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

danieleloff

Adam Maass:

Thanks for the tip, I will remember to be careful what methods I call
from the constructor. It's just unpredictable unless they are private,
static, or final. Thanks for setting me straight on this.

-Dan

Chris said:
I'd suggest that your mind is the one in error here, since what you're
complaining about one of the most fundamental aspects of object-oriented
programming. It's called polymorphism, and without it you aren't
writing OO software.

Mr Smith: It's noteworthy that C++ doesn't have this problem, you can
call public methods from the constructor as long as they are not
declared virtual. That's a much better system in my humble opinion. Is
my mind in error or do you not think that you can write OO software in
C++?
 
C

Chris Smith

Mr Smith: It's noteworthy that C++ doesn't have this problem, you can
call public methods from the constructor as long as they are not
declared virtual. That's a much better system in my humble opinion. Is
my mind in error or do you not think that you can write OO software in
C++?

You obviously meant something different from what you said. I was
responding to "The curious thing about Java is when I override a method
in a derived class, the overridden method is called in the base class."
If you aren't distressed by polymorphism and merely want to control it,
then see the final keyword, which I mentioned in my earlier reply. It's
essentially the opposite of C++'s virtual, except it prevents that
confusing scenario where you have one method hiding another by the same
name.

As for the remaining minor difference between C++ and Java, it's really
quite moot; both languages do things in a way that can have extremely
surprising side-effects. Either way, it becomes necessary to be very
careful about what you do from a constructor.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
O

opalpa

Check out Templete Method pattern for a use of calling derived methods
from base classes. Gosling's choices specifing Java take time to
absorb fully. Initially I found several choices puzzling, and
sometimes I thought them worse. Now that I've explored the language I
claim it to be work of genius.
 
R

Ryan Stewart

The curious thing about Java is when I override a method in a derived
class, the overridden method is called in the base class. I'm hoping
this is somehow my mistake, as I'm new to Java and I cannot imagine why
anyone would do this, but it seems like the behaviour I'm witnessing.
See the thread entitled "Can Java Programmer Learn C++ Quickly?" in this
group for a discussion of this topic.
 
J

jeffc

Mr Smith: It's noteworthy that C++ doesn't have this problem, you can
call public methods from the constructor as long as they are not
declared virtual.

Yes, exactly. In C++, the methods are *not* virtual by default, and of course
in Java they are. So what you're doing in Java is the equivalent of calling
virtual methods from the constructor in C++. Chris mentioned that final methods
would be fine. That means it's not virtual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top