Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphic Methods

 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone explain following code to me?


This is from "A programmer's guide to Java certification" Chapter6 review question 6.25.
The point I dont understand is that since class C extends B, C should have all the methods B has. but method f() in B is set to private that means no way C can override f() in B.
anyway, C surely has method g() inherited from B.
and g() returns method f(), which I think is f() in class C because the method g() here is the one in the C inherited from B.
Please help me out of this confusion.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key here is that private methods are not inherited. Therefore, the method f() in C does not override the method f() in B - it only shadows it. Therefore, as we're treating the object as type B (because of the cast), we invoke the method f() of class B. Had this method been truly overridden, we would have used dynamic binding and invoked the method in class C. But, as private methods are not inherited, there is no overriding occurring and, hence, no dynamic binding.
I hope that helps,
Corey
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method g() is inherited to C from B,
does dynamic method look up try to use the g() in C?
then inside g() method, f() is called. How the dynamic method lookup determines which version of f() to use?
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got confused.
My doubt is about method g() and not method f().
Class C inherits a method g() so when we call ref2.g() arent we going to run the g() in Class C ? And therefore run the f() in class C also ?
Corey, I compiled it and it runs your way, Im just trying to understand...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that class C does not override the method g(). Therefore, it inherits the method from class B. When you invoke method G() on an object of type C, you're really invoking code defined within class B - this is the essence of inheritance.
However, f() is not inherited by class C. Class C defines its own f() method, but this does not cause dynamic binding as no method has been overridden (a method must first be inherited to be overridden). Therefore, when we invoke method f() on the object, it is invoked on class B because that is the compile-time type of the variable ref2.
Check out the JLS, §8.4.6 Inheritance, Overriding, and Hiding for more info.
Just for kicks, try changing the method f() from private to public in class B and see how the behavior changes.
I hope that helps,
Corey
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand it this way:
Whenever a non-static method is inherited, any reference to static or private methods/fields in it will be looked up in the class where this method is defined (i.e super class). In this case, method is defined in class B and inherited in class C. Therefore any calls to private method (f() in this case) will be looked up in super class (class B).
In other words all non-static inherited methods bring with them the implementations of all static and private methods and fields.
Hope this make sense.
Barkat
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, method inheritance does not mean that the code appears in the subclass as if it had been written there as well. It means that the code can be called, from a subclass from where it was declared.
However field inheritance does imply having in the subclass a distinct variable from those in the base class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic