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.
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
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?
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
posted 17 years ago
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
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
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.