aspose file tools*
The moose likes Beginning Java and the fly likes Overriding superclass methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding superclass methods" Watch "Overriding superclass methods" New topic
Author

Overriding superclass methods

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Here's a little problem that's popped up. Let's say I have the following code:
The output is, as would be expected:
2
2
2
However, if I change TwoClass to look like this:
and TestClass to look like:
I get a compiler error stating that OneClass cannot be applied to (int).
Dynamic binding states that at runtime, the JVM runs the implementation based on the class of the object. Variable o is of type OneClass, but it references a TwoClass object, so shouldn't it call the over-ridden getNumber( int i ) method?
Jason
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
Not quite. The thing to remember here is that the thing that identifies a function is the function-name PLUS THE ARGUMENT LIST.
So, in your second example, TwoClass'es "getNumber( int i )" does NOT override OneClass'es "getNumber()" since they have different argument lists, and are therefore different functions.
In fact, TwoClass now has two functions : getNumber() and getNumber(int i).
Of course, OneClass still only has getNumber(), and it isn't affected by anything you've done to TwoClass - hence the compiler error.

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Ah, so when I override, a OneClass variable containing a TwoClass object is going to call TwoClass's implementation. However, by creating the second method, the OneClass variable forces the object to adhere to OneClass's implementation, even though it's a TwoClass reference?
Jason
[This message has been edited by jason adam (edited September 18, 2001).]
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
That's right, but perhaps a better way of saying it is :
Despite creating a second method, the OneClass variable can still only "see" the OneClass attributes the object being referenced.
The point here is that other classes can extend from OneClass, and not all of those will have getNumber(int i). The only things we KNOW that every subclass will have is what is declared in OneClass, so that is all that we're allowed to call.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
The compiler looks at the method signature based on the reference type. So it looks for

public int getNumber( int i )

because your reference is
OneClass o = t ;

The compiler, before even looking at the class type of t looks for a method in OneClass with the signature getNumber( int i ).

OneClass does not have a method with that signature, so you get a compile error before you ever get to the dynamic Runtime binding which looks at the true class of the object assigned to that reference.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Thanks for the clarification both of you, it's definitely helped to further my understanding of the concepts behind the code
Jason
 
 
subject: Overriding superclass methods