EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Inheritance

Computer Science / Java tutorial chapter - Published 2025-12-12 - Java

In Java, constructors are used to initialize the attributes of an object, making the language more reflective of real-world scenarios. By default, Java provides a no-argument constructor that is automatically invoked if no constructors are defined. However, if you create a custom constructor, such as a parameterized one, you must explicitly define the default constructor, as it will no longer be provided automatically.

Note:

If a base class has a no-argument constructor, it is called automatically when a constructor of the derived class is invoked.

Example:

// Java Program to Demonstrate
// Constructor Invocation Without Using super Keyword

// Parent class
class Parent {
    // Constructor of the parent class
    Parent() {
        System.out.println("Parent Class Constructor Called");
    }
}

// Child class
class Child extends Parent {
    // Constructor of the child class
    Child() {
        System.out.println("Child Class Constructor Called");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Creating an object of the child class
        Child childObj = new Child();

        // Note: The constructor of the parent class is invoked first,
        // followed by the constructor of the child class.
    }
}

Output:

Parent Class Constructor Called
Child Class Constructor Called

In Java, constructors are used to initialize the attributes of an object, making the language more reflective of real-world scenarios. By default, Java provides a no-argument constructor that is automatically invoked if no constructors are defined. However, if you create a custom constructor, such as a parameterized one, you must explicitly define the default constructor, as it will no longer be provided automatically.

Note:

If a base class has a no-argument constructor, it is called automatically when a constructor of the derived class is invoked.

Example:

// Java Program to Demonstrate
// Constructor Invocation Using super Keyword

// Base class
class Vehicle {
    int speed;

    // Parameterized constructor of the base class
    Vehicle(int spd) {
        speed = spd;
    }
}

// Subclass
class Car extends Vehicle {
    String model;

    // Constructor of the subclass
    Car(int spd, String mdl) {
        // Using super to call the base class constructor
        super(spd);
        model = mdl;
    }

    // Method to display details
    void displayDetails() {
        System.out.println("Speed: " + speed + ", Model: " + model);
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Creating an object of the subclass
        Car carObj = new Car(120, "Sedan");

        // Displaying details of the car
        carObj.displayDetails();
    }
}

Output:

Speed: 120, Model: Sedan

Explanation:

In this example, the super() keyword is used to call the base class (Vehicle) constructor with a parameter (speed). This call must be the first statement in the subclass (Car) constructor. Afterward, the model attribute is initialized, and the displayDetails() method prints the values of speed and model.

Key Points:

  • The base class constructor is always called before the subclass constructor.
  • The super() keyword is used to explicitly invoke the base class constructor and must be the first line in the subclass constructor.
  • If super() is not used, the default constructor of the base class is invoked automatically.
End of lesson.