• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

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: 22769
130
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
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic