Friday, 10 March 2017

Main Use of Constructor in Java

Case 1. When a class do not have any constructor then compiler will put default constructor with a super() method as first line in that constructor in case of inheritance  to call super class default constructor.
class Test{
                Test(){
                                System.out.println("Super default");
                }
}
public class Test01 extends Test
  {        
               /*Test01()         <-------you can also write  default const that compiler does internally
       {
              super();  <--------------Should be first line in block
       }*/                                                                                                  
                public static void main(String arg[])
                {
                                Test01 tt= new Test01();
   }
}
OP- Super default

Case 2: When you define a  argument constructor in  super class then extending the same class and if you are not define the argument const in sub class then you will get CTE.
class Test{
       int a;
      
       Test(int a)
       {
         this.a=a;
              System.out.println("super parameter value:"+this.a);
       }
}
public class Test01 extends Test
  {     //no argu const
       public static void main(String arg[])
       {
              Test t = new Test(10);
   }
}
CTE:- Implicit super constructor Test() is undefined for default constructor. Must define an explicit constructor.
Here, if you are not defining the argument const in sub class then how will you call the super class respective constructor.
Hence, you have to write the argument const in sub class with super(args) as first line as below code:-
class Test{
       int a;
      
       Test(int a)
       {
         this.a=a;
              System.out.println("super parameter value:"+this.a);
       }
}
public class Test01 extends Test
  {
       Test01(int aa)
       {
              super(aa);
       }
       public static void main(String arg[])
       {
              Test t = new Test(10);
   }
}
OP-super parameter value:10    

or
Write default cont in super class explicitly:

code:-

class Test{
       int a;
       Test()
       {
             
       }
       Test(int a)
       {
         this.a=a;
              System.out.println("super parameter value:"+this.a);
       }
}
public class Test01 extends Test
  {
       public static void main(String arg[])
       {
              Test t = new Test(10);
   }
}
Case 3:We have define a argument constructor in super class and defined  a default const in sub class with super() method as first line then you will get the CTE.
Code:-
class Test{
       int a;
      
       Test(int a)
       {
         this.a=a;
              System.out.println("super parameter value:"+this.a);
       }
}
public class Test01 extends Test
  {
       Test01()             
       {
              super();
       }
       public static void main(String arg[])
       {
              Test t = new Test(10);
   }
}
CTE- The constructor Test() is undefined

Explanation:-Whenever you  not define any const in super class then compiler will put by default it. But if you have a argument const in your super class then compiler will not put any default const into same class. In that case if you are writing a default const in your sub class then you will get a compiler time error because default const is not present in super class by default.
To resolve this issue ,write a default const explicitly in super class.

Code:-
class Test{
       int a;
       Test()
       {
             
       }
       Test(int a)
       {
         this.a=a;
              System.out.println("super parameter value:"+this.a);
       }
}
public class Test01 extends Test
  {
       Test01()
       {
              super();
       }
       public static void main(String arg[])
       {
              Test t = new Test(10);
   }
}




No comments:

Post a Comment

Download Android SDK

Download Android SDK tools Follow me on Instagram - https://www.instagram.com/virenautomationtesting/ Follow me on Twitter- https://twitt...