• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Constructor Chaining

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

int value1;
int value2;

Demo() {
value1 = 1;
value2 = 2;
System.out.println("Inside 1st....... Parent Constructor");
}

Demo(int a) {
value1 = a;
System.out.println("Inside 2nd Parent Constructor");
}

public void display() {
System.out.println("Value1 === " + value1);
System.out.println("Value2 === " + value2);
}

public static void main(String args[]) {
DemoChild d1 = new DemoChild();
d1.display();
}
}
class DemoChild extends Demo {
int value3;
int value4;
DemoChild() {
this(5);// Need explanation why is it calling parent default constructor.
value3 = 3;
value4 = 4;
System.out.println("Inside the Constructor of Child");
}

DemoChild(int a) {
//super();
value3 = 5;
value4 = 6;
System.out.println("Inside the Constructor of 2nd Child");
}

public void display() {

System.out.println("Value1 === " + value1);
System.out.println("Value2 === " + value2);
System.out.println("Value1 === " + value3);
System.out.println("Value2 === " + value4);
}
}

Output of this program is

Inside 1stParent Constructor
Inside the Constructor of 2nd Child
Inside the Constructor of Child
Value1 === 1
Value2 === 2
Value1 === 3
Value2 === 4

I am expecting this(5) in DemoChild() constructor would call DemoChild(int a) . But as per the output it is making first super() call then calling DemoChild(int a). Can anyone please clarify doubt and explain the flow of constructor chaining
if possible.

Thanks
Rama
 
Marshal
Posts: 28492
113
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose that it was DemoChild(int) which was calling the super() constructor. How would the output differ from what you are seeing now?
 
Rama Lakshmi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Suppose that it was DemoChild(int) which was calling the super() constructor. How would the output differ from what you are seeing now?



Thank you Paul.
I understood now. this(5) called DemoChild(int a) whose first line is super(). which called Demo() and printed "Inside 1stParent Constructor " :)


Thanks
Rama
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic