What are constructors?

Java Constructors are the block of codes which we use to initialize an object.

Every class has its own constructor. If we don’t declare the constructor for a class then the compiler initialize a default constructor for that class. Constructors looks like a method but it they don’t have any return type like methods do.

What are the rules for creating constructor?

In Java we need to follow below rules to create constructors.

  • The constructor name must be the same as of the class.
  • The constructor does not have a return type like methods have.

Types of Constructor

There are two types of constructor in Java:

  • Default Constructor
  • Parameterized Constructor

Default Constructor

The Default constructor in Java does not contain any parameters. As discussed earlier, if we don’t specify any constructors in class, Compiler creates the Default constructor.

public class fruits{
	fruits( )
	{    
		System.out.println(“fruit class”);
	}
	public static void main(String args[]){
		fruits f = new fruits( );
	}
}

 

Parameterized Constructor

Parameterized Constructors are like Default constructors but takes arguments or parameters . We need to pass the arguments while calling the Parameterized constructors to create an Object.

We can overload the constructors with different data types as its parameters.


public class vehicle{
    int number;
    String model;
    
    vehicle(int n,String m){
		number = n;
		model = m;
    }
    void display(){
		System.out.println(number+" "+model);
	}
 
    public static void main(String args[]){
		vehicle v1 = new vehicle(1,"BMW");
		vehicle v2 = new vehicle(2,"Mercedes");
		v1.display();
		v2.display();
   }
}

Constructor Overloading

In Java, like methods we can overload constructors, The constructor overloading can be achieved in a class by having different types of parameters and the number of parameters.

Example


public class vehicle{
    int number;
    String model;
    int price;
    vehicle(int n,String m){ //Parametrized Constructor with 2 arguments 
		number = n;
		model = m;
    }
    vehicle(int n,String m,int p){ //Parametrized Constructor with 3 arguments
		number = n;
		model = m;
		price=p;
    }
    void display(){
		System.out.println(number+" "+model+" "+price);
	}
 
    public static void main(String args[]){
		vehicle v1 = new vehicle(1,"BMW",900000);
		vehicle v2 = new vehicle(2,"Mercedes");
		v1.display();
		v2.display();
   }
}

Constructor Chaining

In Java Constructor chaining is a process where one constructor is called from another constructor.
Example

public class Test {
     
    public Test(){
        System.out.println("constructor 1...");
    }
    public Test (int i){
        this(); //Default Constructor called from this constructor
        System.out.println("constructor 2...");
    }
    public Test (int i,int j){
        this(j); //Constructor with single argument is called form this constructor
        System.out.println("constructor 3...");
    }
     
    public static void main(String a[]){
        Test t = new Test (10,20);
    }
}

Super Keyword

The super keyword acts like a reference variable that is used to refer the parent class objects. As we create the instance of the subclass the instance of the parent class is created implicitly.

Why we need super keyword?

The super keyword is needed in the following categories:

  1. The super keyword is used to refer the parent class variable. Let us understand this through an example
    public class Fruits{
      String name="mango";
    }
    
    class Banana extends Fruits{
      String name="banana";
        
      void display(){
       System.out.println(super.name);//it will print name of fruits which is a parent class
      }
      public static void main(String args[]){
       Banana f=new Banana();
       f.display();
       
    }
    }           
    

    In the above example there are two classes Fruits is a parent class whereas banana is a child class and in both the classes the name is a common property. So to access the variable of parent class we have use super keyword. If we don’t use the super keyword, then it will print the child class’s variable.

  2. The super keyword is used to invoke the parent class constructor. Let us have a look at the example
    public class Fruits{
    	Fruits()
    	{        
    		System.out.println("In parent class…");
    	}
    }
    class Banana extends Fruits{
    	Banana(){
    		super();//it will invoke parent class constructor
    		System.out.println("In child class…");
    	}
    	public static void main(String args[]){
    		Banana f=new Banana();      
    	}
    }
    
  3. Super keyword can be used to invoke the parent class’s method. Let us have an example to understand this:
    public class fruits{
    	void msg()
    	{     
    		System.out.println("In parent class");
    	}
    }
    class fruit1 extends fruits{
    	void msg() 
    	{      
    		System.out.println("In child class");
    	}
    	void display(){
    		msg();//it will invoke child class msg() method
    		super.msg();//it will invoke parent class msg() method
    	}
    
    public static void main(String args[]){
    	fruit1 f=new fruit1();
    	f.display();
    	}
    }