Constructor is the member of the class and it has name same as class name. Constructor is the one of the important concept in java which is responsible to initialize the instance member of the class. Its functionality is like a method only  but used by the JVM as a indicator to initialize the instance variable. It is first invoked by JVM while creating the object of the user defined class. And to support inheritance it is calling super class constructor by invoking super() in the subclass constructor to initialize the property of super class. Constructor must not have any return statement as it is not going to return any value because by creating object it is automatically invoke by the JVM and used to initialize the property of the object whose reference value is stored in the reference variable so no need to return any value. 

If we have a constructor defined in our class then JVM is not going going to invoke any default constructor. Only the constructor defined by the user will be used to initialize the property of the object. 

The first statement for nay constructor must either be the this() or super(). Both cannot be used at the same time because this() is used to instruct for the current class and super() is used to instruct the super class. So both cannot used at a time as by invoking super(), the control will not be back to that position. 

Constructor can be overloaded which follows the chain of execution of overloaded constructor and finally make sure that at its last chaining, should call to super class constructor by invoking super(), may be implicitly by the JVM or explicitly by the user otherwise it will give error. 

As it is like a method so we can use throws keyword with it. 

Note : Try not to make any recursive constructor , by which JVM fails to invoke super() to initialize the super class member.And is there is no user defined super class then it will fail to invoke Object class to instruct JVM that , this is the end of the invoking super class constructor. 


What are the access specifier we can use with the constructor?
we can use public, private and protected with the constructor. 
--> public constructor is accessible anywhere.
--> private constructor is only accessible within that class only even it is inherited by the subclass.
--> protected constructor is accessible anywhere within the same package but outside the package it is only accessible to the sub-class which is extending the class which is present in the other package. 

Doesn't seems wonder that how a super class member get initialized by creating the object of the sub-class constructor ?
If we are simply creating the object of subclass then by default JVM will create a no-argument constructor in which super() is the first statement which is responsible for invoking the super-class constructor and that constructor is responsible for initializing the instance member of the super class before using it in sub-class. And finally the chain calling of the super-class constructor get stopped by invoking the Object class constructor which is the super class for all other class in java. 

Use of Constructor in Java
1. It is used to instantiate the instance member of the sub-class and super class instance member.
2. It is used to initialize the instance variable with user value passed at the time of object creation. 
3. It is used to invoke super class constructor by invoking the super(), which must be the first statement of any constructor. 

Some of the situation where compiler insert by default the super() explicitly of not implicitly implemented by the user. 

User defined code                                Compiler defined code 

1. Class A {                                               Class A {
   void A(){                                                 void A(){
    //statement                                               super();     // Inserted by compiler
        }                                                         //statement
    }                                                                }  }
                                                               
2. Class A{}                                              Class A{
                                                                  A(){
                                                                     super(); // Inserted by compiler. 
                                                                      } }

3. Class A {                                               Class A {
    A(int x ) {                                              A(int x ) {
    // statement                                              super() ; // Inserted by compiler
   }  }                                                          // statement        
                                                                  

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic