share

Sunday, 25 January 2015

Default constructor of super class always call in java

Default constructor of super call always call.

         
India.java

package inherintance;

public class India {
    static int _i=0;
    static int _j=0;
    India(int i){
    _j++;
    System.out.println("India parameter constructor call="+_j);
    }
    public India() {
          // TODO Auto-generated constructor stub
    _i++;
    System.out.println("India default constructor call="+_i);
     }
    
}

Dehradun.java

package inherintance;

public class Dehradun extends India {
     Dehradun(){
         
     }

Dehradun (int i){
     System.out.println("Dehradun parameter constructor calli="+i);
}   
     public static void main(String[] args) {
          Dehradun obj=new Dehradun(4);
        System.out.println("I value="+_i);

           System.out.println("==============");

           Dehradun obj1=new Dehradun();
             System.out.println("I value="+_i);
         
     }
    
}

Output:

India default constructor call=1
Dehradun parameter constructor call i=4
I value=1
==========
India default constructor call=2
Dehradun defalt constructor call
I value=2

  

No comments:

Post a Comment