How to Write a Java Program Where a Subclass Constructor Invokes the Constructor of the Superclass

Java is a powerful and versatile programming language that supports object-oriented principles such as inheritance, encapsulation, and polymorphism. One of the fundamental concepts in Java is the ability to create subclasses that extend the functionality of a superclass. This allows for code reuse and a more organized structure. In this article, we'll explore how to write a Java program where a subclass constructor invokes the constructor of the superclass and initializes values.



Understanding Inheritance in Java

In Java, a class can inherit the properties and methods of another class using the extends keyword. The class that is inherited from is called the superclass (or parent class), and the class that inherits is called the subclass (or child class).
Example Scenario

Let's consider an example where we have a superclass Person and a subclass Employee. The Person class will have basic attributes such as name and age, while the Employee class will extend Person by adding an employee ID and a department.
Step-by-Step Implementation
Step 1: Define the Superclass

First, we define the Person class with a constructor to initialize the name and age.

public class Person { private String name; private int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } // Getter methods public String getName() { return name; } public int getAge() { return age; } // Display method public void display() { System.out.println("Name: " + name); System.out.println("Age: " + age); } }

Step 2: Define the Subclass

Next, we create the Employee class that extends Person. The Employee class will have additional attributes for employee ID and department. In the constructor of Employee, we will use the super keyword to invoke the constructor of the Person class.

public class Employee extends Person { private String employeeId; private String department; // Constructor public Employee(String name, int age, String employeeId, String department) { super(name, age); // Invoking superclass constructor this.employeeId = employeeId; this.department = department; } // Getter methods public String getEmployeeId() { return employeeId; } public String getDepartment() { return department; } // Overriding display method @Override public void display() { super.display(); // Calling superclass display method System.out.println("Employee ID: " + employeeId); System.out.println("Department: " + department); } }

Step 3: Create a Main Class to Test the Implementation

Finally, we create a Main class to test our implementation. We'll create an instance of the Employee class and display its details.

public class Main { public static void main(String[] args) { // Creating an instance of Employee Employee emp = new Employee("John Doe", 30, "E12345", "Engineering"); // Displaying employee details emp.display(); } }

Explanation

Superclass Constructor Invocation: The Employee constructor uses the super(name, age) call to invoke the Person constructor, passing the name and age parameters. This ensures that the name and age attributes are initialized by the Person class.

Overriding Methods: The Employee class overrides the display method of the Person class. Inside the overridden display method, it first calls super.display() to print the name and age attributes, and then it prints its own attributes (employeeId and department).

Polymorphism: The display method in the Employee class demonstrates polymorphism by extending the functionality of the Person class's display method.

Conclusion

In this article, we've demonstrated how to write a Java program where a subclass constructor invokes the constructor of the superclass and initializes values. By leveraging inheritance and constructor chaining with the super keyword, we can create a more modular and maintainable codebase. This example illustrates the power and flexibility of object-oriented programming in Java, allowing developers to build upon existing code and extend functionality in a clean and efficient manner.

Happy coding!

Comments