This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71

output: 1

Object is of class C so C's g() is called(which is the one it inherits from B bcoz does not have it's own g()).
Now this g() calls f(). Shouldn't it call C's f() because underlying object is C. Why does it call B's f() ?

thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Notice that in class B the method f() is private. So C has not overridden B's f(). So in B's g() method, the call to method f() is its own private f().

Another thing to notice is that class A's f() method is private, so B and C can't use that method either.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
If i remove private from B's f() then i get the output as 2.
Does that mean first it looks for the overridden f() and if it's not there then B's f() is called?
Can't get the reason for this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding
 
Similar Threads
Dynamic Method Lookup
out put of class
Polymorphism
Polimorphism x Casting
Inheritance doubt