Name _______________________________
The first line of every constructor must be either
this
call to another constructor in the
same class.
super
call to a parent constructor.If no constructor call is written as the first line of a constructor, the compiler automatically inserts a call to the parameterless superclass constructor.
What is printed by this program?
// ConstructorChain.java -- Illustrates constructor chaining. // Fred Swartz - 5 May 2003 ///////////////////////////////////////////////// class ConstructorChain class ConstructorChain { public static void main(String[] args) { Child c = new Child(); } }//end class ConstructorChain //////////////////////////////////////////////////////////// class Child class Child extends Parent { Child() { System.out.println("Child() constructor"); } }//end class Child /////////////////////////////////////////////////////////// class Parent class Parent extends Grandparent { Parent() { this(25); System.out.println("Parent() constructor"); } Parent(int x) { System.out.println("Parent(" + x + ") constructor"); } }//end class Parent ////////////////////////////////////////////////////// class Grandparent class Grandparent { Grandparent() { System.out.println("Grandparent() constructor"); } }//end class Grandparent
You can check your answer with Constructor Chaining Answer.