This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inheritance - weird behaviour ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance - weird behaviour ?" Watch "Inheritance - weird behaviour ?" New topic

Inheritance - weird behaviour ?

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

I have this piece of code:

I do not understand why line marked //1 calls the subclass's
f_() method instead of the superclass's ?


Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Since f_() method is overridden in Child it will get called if the Object is Child no matter where. Becuase of late binding at runtime the method that will be called depends on the acutally Object. Since Child is in used and it has f_() this method will get called.
I agree. Here's the link:
subject: Inheritance - weird behaviour ?
Similar Threads
Constructor doubt??
Constructor doubt
Question for variable initialization order
Project Euler Problem 25
Constructor,why displaying null on this program