Java Constructor

  1. Object creation is not enough compulsory we should perform initialization then only the object is in a position to respond properly.

  2. Whenever we are creating an object some piece of the code will be executed automatically to perform the initialization of an object this piece of the code is nothing but a constructor.

  3. Hence the main objective of the constructor is to perform initialization of an object.

Rules:

  1. The name must be the same as the Class name.

     class Test{
        Test(){
          }
     }
    
  2. Return type concept not allowed for the constructor, not even void.

     class Test{
     //right
     Test(){
     //constructor
     }
     //wrong
     void Test(){
     //method
     }
     }
    
    ๐Ÿ’ก
    It is legal to have a method with the same name as a class but is it good practice? no.
  3. Allowed Modifiers: public, default, protected, private

Not Allowed Modifiers: final, static, synchronized

class Student{
 public Student(){
  //valid
  }
 static Student(){
  //invalid
  } 
}

Default Constructor :

  1. If we are not writing any constructor explicitly then the compiler will automatically create a default constructor.

     class Test{
         //default constructor
     }
    
  2. If we specify or write constructor then the compiler will not create default constructor.

     class Test {
    
      Test(){
      }
     }
    
  3. We can't have both a programmer-written constructor and a compiler-generated constructor.

Prototype or rule of Default constructor

Test(){
}
  1. no argument

  2. access modifier same as class modifiers.

     public class Test(){
       public Test(){
       }
     }
    
     default class Test(){
       default Test(){
       }
     }
    
  3. It contains only one line.

class Test(){
  Test(){
  super();
  }  
}

super() and this() :

  1. first line of constructor should be either super() or this().

  2. if we don't specify then the compiler will add super().

case 1 :

class Test{
 Test(){
  system.out.println("hey");
  super();  //must be first 
 }
}
//compiler error

case 2:

class Test{
 Test(){
  system.out.println("hey");
  super();  //must be first 
 }
}
//compiler error

case 3:

class Test{
 Test(){
  system.out.println("hey");
  super();  //must be first 
 }
}
//compiler error

๐Ÿšง super() โ†’ to call a parent or super class constructor

๐Ÿšง this() โ†’ to call the current class constructor

super() & this() vs super & this :

super and this are keywords used to refer super and current class instance variables.

class Test{
 String s = "parent";
}
class Marks extends Test{
 String s = "child"

 public void m1(){
  System.out.println(s); //child
  System.out.println(this.s); //child 
  System.out.println(super.s); //parent
 }
}

class Per{
 public static void main(String[] args){
  Marks m = new Marks();
 }
}

class Test{
 String s = "parent";
}
class Marks extends Test{
 String s = "child"

 public static void m1(){ //CE : static
  System.out.println(s); 
  System.out.println(this.s);
  System.out.println(super.s); 
 }
}

class Per{
 public static void main(String[] args){
  Marks m = new Marks();
 }
}

Constructor overloading :

๐Ÿšง Same name different arguments.

when a class contains more than 1 constructor with different arguments , these constructors are called as overloaded constructor.

class Test {
 Test(double d){
   this(10);
   System.out.println("double");
 }
 Test(int i){
   this();
   System.out.println("int");
 }
 Test(){
   System.out.println("no argument");
 }
}

public static void main(String[] args){
Test t1 = new Test(20.4);//no argument 
                //int
             //double            
}

Inheritance and Overriding:

๐Ÿšง inheritance is not possible in constructors.

Why?

class Test{
 public void T1(){
   //code
  }
}
class Marks extends Test{
 public void m1(){
   //code
  }
}
class Per{
 public static void main(String[] args){
  Marks m = new Marks();
 }
}
//As we know in inheritance 
//Test can access one method ie T1
//Marks can access 2 methods ie T1 and m1
class Test{
 T1(int i){
   //code
  }
}
class Marks extends Test{
 m1(int j){
   //code
  }
}

class Per{
 public static void main(String[] args){
  Marks m = new Marks();
 }
}
//As we know in inheritance 
//Test can access one method ie T1
//Marks can access 1 methods m1 not T1 thats why inheritance is not possible
1
Subscribe to my newsletter

Read articles from Abhinav Chaudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhinav Chaudhary
Abhinav Chaudhary