aspose file tools*
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
Author

Collections

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 45



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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections
 
Similar Threads
Overriding and Overloading.
Doubt About Access Modifier (Private)
try/catch with exception casting
Overriding and Overloading
a beginner wanna knows something about this( )