Constructor In Java

Constructor is block of code which executed when object of class is created. Constructor is responsible for creating new object. It assigns initial values of object. Syntax of constructor is almost same as method. Constructor has below rule -

  1. Name of constructors must be same as class name.
  2. Constructors must not have return type. Constructor can not return a value.

Types Of Constructor

1. Default Constructor:

A constructor without parameters is called as default constructor. When we don't define any constructor in our class then compiler will add default constructor. It means a class will always has constructor either we explicitly define or compiler will add one.
Example of default constructor-

public class Person {
    public Person() {
 System.out.println("In Default Constructor of Person");
   }
   public static void main(String[] args) {
 Person person = new Person();
   }
}

Output: In Default Constructor of Person

If we remove default constructor in above program then program will compile and run successfully but it will not print anything because compiler will add default constructor implicitly.

Example -

public class Person {
   public static void main(String[] args) {
 Person person = new Person();
   }
}

2. Parameterized Constructor

A constructor with parameters is called as parameterized constructor.

public class Person {
 private String name;
 public Person(String name) {
  System.out.println("Parameterized Constructor");
  this.name = name;
 }
 public static void main(String[] args) {
  Person person = new Person("Prithvi");
 }
}

Once we add parameterized constructor, compiler will not create default constructor. If we want default constructor then we have to define it explicitly.

Below program will give compilation error because we are calling default constructor to create new object. There is a parameter constructor and no default constructor. In this case, compiler will not add default constructor. Line 8 will give error - The constructor Person() is undefined.

public class Person {
 private String name;
 public Person(String name) {
  System.out.println("Parameterized Constructor");
  this.name = name;
 }
 public static void main(String[] args) {
  Person person = new Person();
 }
}

We can resolve this error by either adding default constructor, calling parameterized constructor or removing parameterized constructor.

public class Person {
 private String name;
 public Person() {
  System.out.println("default Constructor");
 }
 public Person(String name) {
  System.out.println("Parameterized Constructor");
  this.name = name;
 }
 public static void main(String[] args) {
  Person person1 = new Person();
  Person person2 = new Person("Prithvi");
 }
}

Both objects will be created successfully in above program. We have defined default and parameterized constructor, and creating person1 and person2 by calling both constractors.

Inheritance And Constructor

Super class constructor must be called from Derived class constructor using super() keyword. super statement must be first line of derived class constructor. Derived class constructor will always add super statement to call default constructor of Parent class. If there is a parameterized constructor in parent class but not default constructor then it is mandatory to call parent class constructor explicitly from child class constructor otherwise you will get compilation error - Implicit super class constructor Parent() is undefined. Must explicitly invoke another constructor. Cause of error is compiler will not add default constructor if we provide parameterized constructor.

Below program demonstrates Child class constructor is calling Parent class constructor implicitly.   

class Parent{ 
 public Parent() {
  System.out.println("Constructor of Parent");
 }
}

class Child extends Parent{ 
 public Child() {
  System.out.println("Constructor of Child");
 }
}
public class Demo {
 public static void main(String[] args) {
  Child child = new Child();  
 }
}

Output of above program -
Constructor of Parent
Constructor of Child

Below program is to demonstrate how Child class constructor give compilation error if Parent class has parameterized constructor but not default constructor and Child class is not calling Parent class constructor.


class Parent{
 public Parent(String name) {
  System.out.println("Constructor of Parent" + name);
 }
}

class Child extends Parent{
 public Child() {//Compilation error on  this line
  System.out.println("Constructor of Child");
 }
}
public class Demo {
 public static void main(String[] args) {
  Child parent = new Child(); 
 }
}

We can solve this error by calling Parent class constructor from Child class constructor. Below program will demonstrate this -

class Parent{ 
 public Parent(String name) {
  System.out.println("Constructor of " + name);
 }
}

class Child extends Parent{
 public Child() {
  super("Super");
  System.out.println("Constructor of Child");
 }
}
public class Demo {
 public static void main(String[] args) {
  Child parent = new Child();  
 }
}
Output -
Constructor of Super
Constructor of Child

Below program will show compilation error because Parent class constructor is not called from first line of Child class constructor. Error - Constructor must be the first statement in a constructor.


class Parent{
 public Parent(String name) {
  System.out.println("Constructor of " + name);
 }
}

class Child extends Parent{
 public Child() {// Error on this line
  System.out.println("Constructor of Child");
  super("Super");// Error on this line
 }
}
public class Demo {
 public static void main(String[] args) {
  Child parent = new Child();
 }
}

Comments

Post a Comment

Popular posts from this blog

Data types and Literals in Java

How to define Auxiliary Constructor in case class in Scala

Dependency Injection in Spring framework