In: Computer Science
Experiment with a simple derivation relationship between two classes. Put println statements in constructors of both the parent and child classes. Do not explicitly call the constructor of the parent in the child. What happens? Why? Change the child’s constructor to explicitly call the constructor of the parent. Now what happens?
class Parent{
Parent(){
System.out.println("Parent");
}
}
class Child extends Parent{
Child(){
System.out.println("Child");
}
}
public class TestRelation {
public static void main(String[] args) {
Child c = new Child();
}
}
Here first it will call the child class constructor from there before executing the child class constructor it will call the parent class default constructor after completing the execution of parent class constructor child class constructor will call
class Parent{
Parent(){
System.out.println("Parent");
}
}
class Child extends Parent{
Child(){
super();
System.out.println("Child");
}
}
public class TestRelation {
public static void main(String[] args) {
Child c = new Child();
}
}
No Change, here also same output as we explicitly calling the parent class default constructor using super. here we can call any of parent class constructor like one arg,2 arg constructors
Note : If you like my answer please rate and help me it is very Imp for me