Trying to figure out when the super () method is being called. In the code below, the child class has a no-argument constructor with this (), so the compiler cannot insert super (). Then how the parent constructor is called.
public class Parent
{
public Parent()
{
System.out.println("In parent constructor");
}
}
public class Child extends Parent
{
private int age;
public Child()
{
this(10);
System.out.println("In child constructor with no argument");
}
public Child(int age)
{
this.age = age;
System.out.println("In child constructor with argument");
}
public static void main(String[] args)
{
System.out.println("In main method");
Child child = new Child();
}
}
Exit:
In main method
In parent constructor
In child constructor with argument
In child constructor with no argument
source
share