File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections" Watch "Collections" New topic


Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46

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

Joined: Aug 06, 2012
Posts: 333

You are overriding disp() method, so method call in constructor this.disp() refers current executing object.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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:
subject: Collections
It's not a secret anymore!