aspose file tools*
The moose likes Beginning Java and the fly likes Constructor Chaining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor Chaining" Watch "Constructor Chaining" New topic
Author

Constructor Chaining

Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24


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
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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.
Faisal A Khan
Greenhorn

Joined: Jul 02, 2010
Posts: 11
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Welcome to the Ranch

Have you tried it?
Faisal A Khan
Greenhorn

Joined: Jul 02, 2010
Posts: 11
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
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor Chaining