Sunday, March 5, 2017

Order of Execution of Constructors Java

Tags

Constructors follow the order of derivation in case of Inheritance
i.e- Superclass to Subclass
firstly superclass constructor is called then subclass constructor is called.
also in case of Multilevel inheritance.

Following code will illustrate the concept:

Code::

class a
{
    a()
    {
        System.out.println("A's Constructor");
    }
}
class b extends a
{
    b()
    {
        System.out.println("B's Constructor");
    }
}
class c extends b
{
    c()
    {
        System.out.println("C's Constructor");
    }
}
public class Constructors
{

    public static void main(String[] nt)
    {
        c ob=new c();
    }

}

Output::