• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are overriding disp() method, so method call in constructor this.disp() refers current executing object.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic