Tuesday, April 5, 2011

A subclass constructor frist call his super class constructor


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
    


For Further Reading,
Java

0 comments:

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.