Constructor

A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects, to required,or default values at the time of object creation. It is not mandatory for the coder to write a constructor for the class.

If there is no user defined constructor provided for a class, compiler initializes member variables to its default values.

  • numeric data types are set to 0
  • char data types are set to null character(' ')
  • reference variables are set to null

In order to create a Constructor there are some rules to follow

  • It has the same name as the class
  • It should not return a value not even void

Creating First Constructor

  • Example
  • class Example
    {
    	int  value1;
    	int  value2;
    	Example()
    	{
    		value1 = 10;
    		value2 = 20;
    		System.out.println("Inside Constructor");
    	}
    	public void display()
    	{
    		System.out.println("Value1 === "+value1);
    		System.out.println("Value2 === "+value2);
    	}
    	public static void main(String args[])
    	{
        	Example d1 = new Example();
        	d1.display();
    	}
    }
    		

Constructor Overloading

Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists. Constructor overloading in java allows to have more than one constructor inside one Class.

Constructor overloading is not complex you just need to create another constructor, obviously same name as of class but different signature but there are certain rules related to Constructor overloading which needs to be remembered while overloading constructor in Java.


Overloading Rules :

  • First and foremost rule to overload a method in Java is to change method signature. Method signature is made of number of arguments, type of arguments and order of arguments if they are of different types.
  • Return type of method is not part of method signature, so just changing the return type will not overload method in Java.

  • Example :
    class Example
    {
          int  value1;
          int  value2;
          Example()
          {
           	value1 = 10;
           	value2 = 20;
           	System.out.println("First Constructor");
          }
          Example(int a)
          {
          	value1 = a;
          	System.out.println("Second Constructor");
          }
          Example(int a,int b)
          {
          	value1 = a;
          	value2 = b;
          	System.out.println("Third Constructor");
    	  }
    	  public void display()
    	  {
    	  	System.out.println("Value1 "+value1);
          	System.out.println("Value2 "+value2);
    	  }
    	  public static void main(String args[])
    	  {
    	  	Example d1 = new Example();
    	  	Example d2 = new Example(30);
    		Example d3 = new Example(30,40);
    		d1.display();
    		d2.display();
    		d3.display();
     	  }
    }
    		

Constructor Chaining

If we consider a scenario in which a base class is extended by a child class. Whenever an object of the child class is created , the constructor of the parent class is invoked first.This is called Constructor chaining.

  • Example :
    class Example
    {
       int  value1;
       int  value2;
        Example()
        {
          value1 = 1;
          value2 = 2;
          System.out.println("First Parent Constructor");
        }
        Example(int a)
        {
          value1 = a;
          System.out.println("Second Parent Constructor");
        }
        public void display()
        {
        	System.out.println("Value1 "+value1);
        	System.out.println("Value2 "+value2);
        }
      	public static void main(String args[])
      	{
        	ExampleChild d1 = new ExampleChild();
        	d1.display();
        }
    }
    class ExampleChild extends Example{
        int value3;
        int value4;
        ExampleChild()
        {
        	//super(1);
         	value3 = 3;
         	value4 = 4;
        	System.out.println("Child Constructor");
        }
        public void display()
        {
            System.out.println("Value1  "+value1);
            System.out.println("Value2  "+value2);
            System.out.println("Value1  "+value3);
            System.out.println("Value2  "+value4);
       }
    }
    		

Use of this for Constructor

Use of This For constructor .

  • Example :
    public class Rectangle {
        private int x, y;
        private int width, height;
            
        public Rectangle() {
            this(0, 0, 0, 0);
        }
        public Rectangle(int width, int height) {
            this(0, 0, width, height);
        }
        public Rectangle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
        ...
    }
    		
  • Example2 :
    class Loan{
        private double interest;
        private String type;
      
        public Loan(){
           this("personal loan"