Constructor in Inheritance in Java

A class in Java can inherit traits and behaviors from another class thanks to the robust inheritance system. Constructors are essential for initializing objects and maintaining the correct operation of the class hierarchy when dealing with inheritance. In this section, we will examine constructor functionality in Java's inheritance context and provide thorough explanations and examples.

Constructor Fundamentals

The constructor is the unique process responsible for initialising the class object. The new keyword generates an object with the same name as the main class when it is called. Inheritance is the process of building a subclass in Java that gets properties and methods from a superclass. The superclass constructor is called right away upon the creation of a subclass, guaranteeing that the inherited properties are initialised correctly.

Constructor Chaining

A constructor can invoke another constructor in the same class or a superclass, a feature known as constructor chaining. This guarantees that, in the case of inheritance, the constructors of every class in the hierarchy-from the top superclass to the instantiated subclass-are called.

Constructor.java

Output:

Animal constructor called
Dog constructor called
Buddy is eating
Buddy is barking

In this case, the constructor of the Animal class is automatically called first using super(name) to initialize the name attribute when a Dog object is created. The initialization of the breed property is then completed by calling the constructor of the Dog class.

Default Constructors in Inheritance

Java offers a default constructor with no parameters if a class does not declare one explicitly. If the superclass does not have a no-argument constructor, the subclass constructor must use super() to explicitly call one of the superclass constructors when the subclass is constructed.

Constructor.java

Here, the color property is correctly initialized since the Circle class uses super(color) to explicitly invoke the Shape class's constructor.

Constructor Overloading in Inheritance

Multiple constructors in a class are possible in Java; it is referred to as constructor overloading. Every constructor in the hierarchy may be overloaded when working with inheritance to offer more freedom when creating objects.

Implicit Super Call

In Java, the compiler automatically inserts a call to the default constructor of the superclass as the first statement in the subclass constructor if a constructor in the subclass does not explicitly call a constructor in its superclass using super. The implicit super call is the term for this. Compilation issues occur, however, if the subclass constructor does not call the superclass explicitly and the superclass contains a default constructor.

Constructors of Superclasses and Initialization Order

When a subclass object is formed, the constructors of both the subclass and its superclass are executed in a certain order. Always call the superclass constructor before the subclass constructor. It ensures that superclass components are initialised first. The following is the sequence of execution:

  • In the order of appearance, superclass static variables and static blocks.
  • In the order of appearance, subclass static variables and static blocks.
  • Instance variables and instance blocks of superclasses (in order of appearance).
  • Constructor of superclasses.
  • In order of presentation, subclass instance variables and instance blocks.
  • Constructor for subclasses.





Latest Courses