What will be the output of the following program?
class Base {//w w w.j av a2s. c o m public Base() { System.out.println("Base"); } } class Derived extends Base { public Derived() { System.out.println("Derived"); } } class DeriDerived extends Derived { public DeriDerived() { System.out.println("DeriDerived"); } } class Test { public static void main(String []args) { Derived b = new DeriDerived(); } }
a) Base/*from w w w.ja v a 2 s .c o m*/ Derived DeriDerived b) Derived DeriDerived c) DeriDerived Derived Base d) DeriDerived Derived
a)
Whenever a class gets instantiated, the constructor of its base classes (the constructor of the root of the hierarchy gets executed first) gets invoked before the constructor of the instantiated class.