Explain different types of constructors.


 Constructors are special member functions whose task is to initialize the objects of its class. It is treated as a special member function because its name is the same as the class name. Java constructors are invoked when their objects are created. It is named such because, it constructs the value, i.e., provide data for the object, i.e., they are used to initialize objects. Every class has a constructor when we don't explicitly declare a constructor for any java class the compiler creates a default constructor for that class which does not have any return type. The constructor in Java cannot be abstract, static, final or synchronized and these modifiers are not allowed for the constructor.


  • Characteristics of Java Constructors

  • An interface cannot have the constructor.
  • Constructors cannot be private.
  • A constructor cannot be abstract, static, final, native, strictfp, or synchronized
  • A constructor can be overloaded.
  • Constructors cannot return a value.
  • Constructors do not have a return type; not even void.
  • An abstract class can have the constructor.
  • Constructors name must be similar to that of the class name inside which it resides.
  • Constructors are automatically called when an object is created.

Instance variables and methods of a class are known as members of a class. Constructors are not members. For this reason, constructors cannot be inherited; but can be accessed by a subclass. Java constructors do not get inherited; only their members (variables and methods) get inherited. So declaring a constructor as final is useless and has no meaning as constructors cannot be overridden. Again, Java constructors should not be synchronized as it locks the object when created and hence, as long as the object is not created any other object can be instantiated.

Types of Java Constructors

There are two types of constructors:

  • Default constructor (no-arg constructor)
  • Parameterized constructor

Default Constructor

A constructor having no parameter is known as default constructor and no-arg constructor.

The structure of a default constructor is like this:

class Demo
{
   public Demo()
  {
   System.out.println("This is a default constructor");
  }
}

The syntax of default constructor:

Syntax:
<class_name>(){

}

Parameterized Constructor

A constructor having an argument list is known as a parameterized constructor. Parameterized constructors are used to supply dissimilar values to the distinct objects.

The structure of a parameterized constructor in Java is:

class Demo
{
 public Demo(int num, String str)
  {
   System.out.println("This is a parameterized constructor");
  }
}

Program for Default Constructor

Example:
import java.util.*;
import java.lang.*;
import java.io.*;
class clerk{
 int roll=101;
   String grade="Manager";
   void display(){System.out.println(roll+" "+grade);}
   public static void main(String args[]){
   clerk c1=new clerk();
   clerk c2=new clerk();
  c1.display();
  c2.display();
 }
}

Program for Parameterized Constructor

Example:
import java.util.*;
import java.lang.*;
import java.io.*;
 class paramC{
 paramC(int a, int b){
  System.out.print("Parameterized Constructor");
  System.out.println(" having Two parameters");
 }
  paramC(int a, int b, int c){
  System.out.print("Parameterized Constructor");
  System.out.println(" having Three parameters");
 }
  public static void main(String args[]){
  paramC pc1 = new paramC(12, 12);
  paramC pc2 = new paramC(1, 2, 13);
 }
}