Java Program to Call One Constructor from another

   
Java Program to Call One Constructor from another

What is Constructor in Java ?

In Java, a constructor is a special method that is used to initialize an object when it is created. Constructors have the same name as the class and have no return type. They are automatically called when an object is created, and can be used to set initial values for the object’s properties. Constructors can also be overloaded, which means that a class can have multiple constructors with different parameter lists.

Ways to Call a Constructor In Java :

  1. Using the “new” keyword: This is the most common way to call a constructor. The “new” keyword is used to create a new instance of the class, and the constructor is called to initialize the object.
  2. Using the “this” keyword: The “this” keyword can be used to call another constructor from within a constructor. This is known as constructor chaining.
  3. Using the “super” keyword: The “super” keyword can be used to call a constructor from the superclass of the current class.
  4. Using Class.newInstance(): This method can be used to create a new instance of a class, and the constructor is called automatically.
  5. Using Constructor.newInstance(): This method can be used to create a new instance of a class, and the constructor is called automatically. This method is more powerful than Class.newInstance() and allows passing arguments to the constructor.

Example of a simple constructor written in pseudo code:

class MyClass {
    int x;
    int y;

    // constructor
    MyClass() {
        x = 0;
        y = 0;
    }
}

Note : In this example, the constructor for the “MyClass” class initializes the values of the “x” and “y” properties to 0 when a new instance of the class is created. The constructor has no parameters and doesn’t return any value.

Example of a constructor with parameters :

class MyClass {
    int x;
    int y;

    // constructor
    MyClass(int a, int b) {
        x = a;
        y = b;
    }
}

Note : This constructor takes two integer parameters, and assigns them to the properties x and y when a new instance of the class is created.

Example 1 :  Calling of one Constructor form another Constructor 

Run

class MyClass {
    int x;
    int y;

    // first constructor
    MyClass() {
        this(0, 0); // call the second constructor
        System.out.println("First constructor called");
    }

    // second constructor
    MyClass(int a, int b) {
        System.out.println("Second constructor called");
        x = a;
        y = b;
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(); // call the first constructor
        MyClass obj2 = new MyClass(1, 2); // call the second constructor
    }
}

Output :

Second constructor called
First constructor called
Second constructor called

Explanation : 

In this example the first constructor calls the second constructor using the “this” keyword, which initializes the properties x and y and then prints “Second constructor called”. After the second constructor is finished executing, control is returned to the first constructor and it then prints “First constructor called”.

This behavior is because the second constructor is called before any other statement in the first constructor, but the constructor does not return anything, the execution continues on the first constructor after the call to second constructor, and the print statement is executed.

Example 2 : Calling  superclass from the constructor of the child class

Run

class SuperClass {
    int x;

    SuperClass() {
        x = 0;
        System.out.println("SuperClass constructor called");
    }
    SuperClass(int a) {
        x = a;
        System.out.println("SuperClass constructor with parameter called");
    }
}

class SubClass extends SuperClass {
    int y;

    SubClass() {
        super(); // call the default constructor of the superclass
        y = 0;
        System.out.println("SubClass constructor called");
    }
    SubClass(int a,int b) {
        super(a); // call the constructor of the superclass with parameter
        y = b;
        System.out.println("SubClass constructor with parameters called");
    }
}

public class Main {
    public static void main(String[] args) {
        SubClass obj1 = new SubClass(); // call the default constructor of the subclass
        SubClass obj2 = new SubClass(1,2); // call the constructor of the subclass with parameters
    }
}

Output :

SuperClass constructor called
SubClass constructor called
SuperClass constructor with parameter called
SubClass constructor with parameters called

Explanation : 

In this example when the default constructor of the SubClass is called, it first calls the default constructor of the SuperClass using the super() keyword, which initializes the property x to 0 and prints “SuperClass constructor called”. Then the SubClass constructor continues its execution, initializing the property y to 0 and printing “SubClass constructor called”.

When the constructor of the SubClass with parameters is called, it first calls the constructor of the SuperClass with a parameter using the super(a) keyword, passing the value of the first parameter. The SuperClass constructor is called and initializes the property x to the passed parameter and prints “SuperClass constructor with parameter called”. Then the SubClass constructor continues its execution, initializing the property y to the second passed parameter and printing “SubClass constructor with parameters called”

You can see that the super class’s constructor is called first and then the subclass constructor continues its execution.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription