This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections" Watch "Collections" New topic
Author

Collections

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.


OCA7
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Collections
 
Similar Threads
Doubt About Access Modifier (Private)
Overriding and Overloading
try/catch with exception casting
Overriding and Overloading.
a beginner wanna knows something about this( )