Java Single Inheritance

Introduction

Single inheritance is a fundamental concept in Java’s Object-Oriented Programming (OOP). It allows a class (subclass or derived class) to inherit fields and methods from another class (superclass or base class). This enables code reusability and helps to create a hierarchical classification.

Table of Contents

  1. What is Single Inheritance?
  2. Benefits of Single Inheritance
  3. Implementing Single Inheritance in Java
  4. The super Keyword
  5. Method Overriding
  6. Real-World Analogy
  7. Example: Single Inheritance
  8. Conclusion

1. What is Single Inheritance?

Single inheritance is when a class inherits from only one superclass. This means a subclass can have only one direct superclass. Single inheritance helps to derive new classes that share common properties and behaviors of the existing class.

2. Benefits of Single Inheritance

  • Code Reusability: Allows a subclass to reuse the fields and methods of the superclass, reducing code redundancy.
  • Simplicity: Simplifies the class hierarchy by maintaining a clear and straightforward inheritance path.
  • Easy Maintenance: Changes in the superclass can be easily propagated to subclasses, making maintenance easier.

3. Implementing Single Inheritance in Java

In Java, single inheritance is implemented using the extends keyword.

Syntax:

class Superclass {
    // fields and methods
}

class Subclass extends Superclass {
    // additional fields and methods
}

4. The super Keyword

The super keyword in Java is used to refer to the immediate superclass of a subclass. It can be used to:

  • Call the superclass’s constructor.
  • Access superclass methods.
  • Access superclass fields.

Example: Using super Keyword

public class Animal {
    String name;

    // Constructor
    public Animal(String name) {
        this.name = name;
    }

    public void display() {
        System.out.println("Animal: " + name);
    }
}

public class Dog extends Animal {
    String breed;

    // Constructor
    public Dog(String name, String breed) {
        super(name); // Call to superclass constructor
        this.breed = breed;
    }

    public void display() {
        super.display(); // Call to superclass method
        System.out.println("Breed: " + breed);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Buddy", "Golden Retriever");
        myDog.display();
    }
}

Output:

Animal: Buddy
Breed: Golden Retriever

5.() Method Overriding

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The overridden method in the subclass must have the same signature as the method in the superclass.

Example: Method Overriding

public class Animal {
    public void sound() {
        System.out.println("This animal makes a sound.");
    }
}

public class Dog extends Animal {
    @Override
    public void sound() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound(); // Calls the overridden method in Dog class
    }
}

Output:

The dog barks.

6. Real-World Analogy

Think of a simple inheritance relationship like that between vehicles and cars. A vehicle is a general class with common properties like speed and capacity. A car is a specific type of vehicle that inherits these properties but can also have additional features like air conditioning and a sunroof.

7. Example: Single Inheritance

Let’s create a more detailed example of single inheritance in Java.

Example: Single Inheritance

// Superclass
public class Employee {
    String name;
    int employeeId;

    // Constructor
    public Employee(String name, int employeeId) {
        this.name = name;
        this.employeeId = employeeId;
    }

    // Method to display employee details
    public void displayDetails() {
        System.out.println("Name: " + name + ", Employee ID: " + employeeId);
    }
}

// Subclass
public class Manager extends Employee {
    String department;

    // Constructor
    public Manager(String name, int employeeId, String department) {
        super(name, employeeId); // Call to superclass constructor
        this.department = department;
    }

    // Method to display manager details
    public void displayDetails() {
        super.displayDetails(); // Call to superclass method
        System.out.println("Department: " + department);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of the Manager class
        Manager manager = new Manager("Alice", 101, "HR");
        // Displaying manager details
        manager.displayDetails();
    }
}

Output:

Name: Alice, Employee ID: 101
Department: HR

In this example, the Manager class inherits from the Employee class, demonstrating single inheritance. The Manager class adds its own field (department) and overrides the displayDetails method to include department information.

8. Conclusion

Single inheritance in Java allows a class to inherit properties and behaviors from a single superclass. This promotes code reusability and helps in organizing classes in a hierarchical manner. By understanding and implementing single inheritance, you can create more modular, maintainable, and efficient Java applications. The super keyword and method overriding are essential aspects of inheritance that enable subclasses to extend and customize the behavior of their superclasses.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top