In the code below, the output is : S1S2. Why do we get the that result?
public class S1 {
public static void main(String[] args) {
new S2();
}
S1(){
System.out.print("S1");
}
}
class S2 extends S1{
S2(){
System.out.print("S2");
}
}
Since S2 extends S1, it’s equivalent to calling all the constructors in a top-level down order.
Java will first create the parent object, S1 and call it’s constructor. Then move down to the next object, S2 with it’s constructor.