File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



In this program i thought it would print SuperClass and 9.0
but is printing 0.0 and 9.0 ,how can anyone tell
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are overriding disp() method, so method call in constructor this.disp() refers current executing object.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When MySuper obj= new MySub(); executing,

1. it calls MySub's default constructor, then

2. MySub's default constructor calls MySuper's Constructor. now,

3. here you are calling disp() , actually this method call is happening on *this* reference of MySub.

4. now it calls MySub's disp method. right now still JVM not assigned value for i in MySub. because still super class constructor is running.

5. hence, the result.

Conclusion: dont call non-private and non-final methods in constructor. if the class can be sub classed.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic