File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphic Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphic Methods" Watch "Polymorphic Methods" New topic

Polymorphic Methods

Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
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.

"If anything can go wrong, it will"
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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,

SCJP Tipline, etc.
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
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

Joined: Apr 20, 2003
Posts: 168
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...

Giselle Dazzi<br />SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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,
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Don't get me started about those stupid light bulbs.
subject: Polymorphic Methods
Similar Threads
Overridding private methods
Polimorphism x Casting
Dynamic Method Lookup
Inheritance doubt