• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding superclass methods

 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic