This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method overriding in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Method overriding in Java" Watch "Method overriding in Java" New topic
Author

Method overriding in Java

Tharindu Priyath
Greenhorn

Joined: Nov 01, 2012
Posts: 4

Output of this program is 0.Why?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
You need to work out the order of invocation of the constructors. Write down which constructor is called and when. Then you can see what of the B object exists. The details are in the Java Language Specification. You will have to follow the links.

By the way: if this is an exam question (original or changed), you must tell us where it is from. It also shows the danger of calling methods not marked final or private from a constructor. It also shows the confusion which can occur when you have two fields of the same name. I hope you are posting this as an example of code you would never use in real life.
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 373
    
    1

Yes, please do tell us where this nightmare came from. I had to step through it with the debugger three times before I understood why its output is what it is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method overriding in Java
 
Similar Threads
Confused with static methods
Inherittance
Regex and split method question
Redefining a static method in subclass
Anonymous inner class