File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding superclass methods

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification both of you, it's definitely helped to further my understanding of the concepts behind the code
Jason
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic