• 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

Constructor Chaining

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If the above code is analyzed then the output would be


(1) Person's no-arg constructor is invoked
(2) Invoke Employee's overloaded constructor
(3) Employee's no-arg constructor is invoked
(4) Faculty's no-arg constructor is invoked

It is because of the concept of Constructor Chaining where a superclass's constructor is called before the subclass's constructor.

In this example first the person and then employee and then the faculty "NO ARGUMENT" constructors are invoked.
My question is if it is necessary that the no arg constructor of sub class can invoke only the no argument constructor of the super/parent class?
Will the output of the code change if the person no arg constructor is line 23 is replaced by the person constructor with some arg?


Appreciate the quick response on this doubt.

~ Faraz
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My question is if it is necessary that the no arg constructor of sub class can invoke only the no argument constructor of the super/parent class?

The quick response is no.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q2: Will the output of the code change if the person no arg constructor is in line 23 is replaced by the person constructor with some arg?

If you replace the no arg constructor with one with an arg(s), you will get a compilation error. You will either have to call the new arg constructor from Employee(String s) using super(...) or you can just re-insert a no arg constructor back in Person along with the arg one.

Just keep in mind that if you do the latter, your arg constructor will not be used (unless you add super to choose) i.e. the output will be the same. However, the former will print whatever you want printed.

In both cases, the order of the constructor messages will be the same regardless of the message.

Hope this makes it clearer. Please let me know.
 
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you tried it?
 
Faisal A Khan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks for welcoming me - assumed it's me as I just joined this month.

Not sure if the question is for me. But I have tried it.

Regards
 
Campbell Ritchie
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Faisal A Khan wrote:Hi Campbell,

Thanks for welcoming me - assumed it's me as I just joined this month.

Yes, it was for you

Not sure if the question is for me. But I have tried it.

Regards

Not really for you, more Original Poster . . . but what happened?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic