Object Creation of Inherited Class in Java



The constructor is something responsible for the object creation of a particular class in Java. Along with other functions of the constructor, it also instantiates the properties/instances of its class. In Java by default super() keyword is used as the first line of the constructor of every class, here the purpose of this method is to invoke the constructor of its parent class so that the properties of its parent get well instantiated before subclass inherits them and use.

The point that should remember here is when you create an object the constructor is called but it is not mandatory that whenever you call a constructor of a class an object of that class is created. As in the above case, the constructor of a parent is called from the constructor of the subclass but the object of the subclass is created only, as the subclass is in is-a relation with its parent class.

Steps to create object of an Inherited class

Following are the steps for Java object creation of Inherited class ?

  • First we will start by creating a parent class that has a constructor that outputs the class name and a message.
  • We will define an extension of the parent class in a subclass.
  • Include a constructor in the subclass that outputs the class name and a message.
  • Create a subclass object in the main procedure. Observe that the subclass constructor is called after the parent class constructor.
  • Verify that the parent constructor is called before the subclass constructor by running the application and observing the output.

Java program to create object of an Inherited class

Below is the Java program for Java object creation of Inherited class ?

class InheritanceObjectCreationParent {
    public InheritanceObjectCreationParent() {
        System.out.println("parent class constructor called..");
        System.out.println(this.getClass().getName()); //to know the class of which object is created.
    }
}
public class InheritanceObjectCreation extends InheritanceObjectCreationParent {
    public InheritanceObjectCreation() {
        //here we do not explicitly call super() method but at runtime complier call parent class constructor
        //by adding super() method at first line of this constructor.
        System.out.println("subclass constructor called..");
        System.out.println(this.getClass().getName()); //to know the class of which object is created.
    }
    public static void main(String[] args) {
        InheritanceObjectCreation obj = new InheritanceObjectCreation(); // object creation.
    }
}

Output

parent class constructor called..
InheritanceObjectCreation
subclass constructor called..
InheritanceObjectCreation

Code Explanation

In this Java program, we will create a parent class InheritanceObjectCreationParent with a constructor that prints a message and the name of our class. Now we can see that the subclass InheritanceObjectCreation extends the parent class and also has a constructor that outputs the name of the class along with a message . Although we don't specifically call super() keyword the Java compiler automatically invokes the parent class constructor when an object of the subclass is created. By doing this, it is ensured that the parent class constructor initialises the parent properties first, running before the subclass constructor.

Updated on: 2024-08-22T12:12:28+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements