File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overriding in Java

 
Tharindu Priyath
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output of this program is 0.Why?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 987
10
C++ Java Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic