When a class constructor call by it self then if it inherited ,call first his super class constructor...
ex. class A //a super class
{
A() // con. of super class
{System.out.println("i m in A");
}
}
class B extends A // sub class of A and super of C
{
B() // con. of B
{System.out.println("i m in b");
}
}
class C extends B // sub class of B
{
C() // con. of C
{
System.out.println("i m in c");
}
}
class Main
{
public static void main(String args[])
{
C a=new C(); // call the con. of class C
}
}
explanation: when C() is call it call the constructor of its super class con. B().
and when B() is call it call the super class con A().
then by C() all super classes cons is call.
Output:
i m in A
i m in B
i m in C
ex. class A //a super class
{
A() // con. of super class
{System.out.println("i m in A");
}
}
class B extends A // sub class of A and super of C
{
B() // con. of B
{System.out.println("i m in b");
}
}
class C extends B // sub class of B
{
C() // con. of C
{
System.out.println("i m in c");
}
}
class Main
{
public static void main(String args[])
{
C a=new C(); // call the con. of class C
}
}
explanation: when C() is call it call the constructor of its super class con. B().
and when B() is call it call the super class con A().
then by C() all super classes cons is call.
Output:
i m in A
i m in B
i m in C
For Further Reading,
0 comments:
Post a Comment