• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Polymorphic call to method from constructor?

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take a look at the following code:




I would expect the output to be Parent's doStuff(), but actually it is Child's doStuff().

Why is it so? I understand the concept of polymorphic call to child's method if invoked on a child instance, but here it is just call from a constructor to a method of the same class. So why is it going to the child's method?

Thanks,
Nidhi
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nidhi Sar wrote:Please take a look at the following code:




I would expect the output to be Parent's doStuff(), but actually it is Child's doStuff().

Why is it so? I understand the concept of polymorphic call to child's method if invoked on a child instance, but here it is just call from a constructor to a method of the same class. So why is it going to the child's method?

Thanks,
Nidhi



Because, even though it is the super class constructor, it is still a Child instance. Java will find the most specific method it can, and run that one. Since you have a Child instance, which has its own doStuff() method, Java will use that one.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks so much for your answer!

That makes sense.

So the call to doStuff() in the constructor actually is this.doStuff() and inside the Parent's constructor this refers to the instance of Child's class upcast to the Parent class! Is that correct?
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Keep in mind that fields of the Child instance are not yet initialized. That might cause problems if you expect them to be initialized already.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic