☕ Java Q7 / 105

Explain the concept of inheritance in Java.

AI-Powered Answer ✓ Answered

Inheritance is a fundamental principle of Object-Oriented Programming (OOP) in Java. It allows one class to acquire the fields and methods of another class. The main purpose of inheritance is to promote code reusability and establish an "IS-A" relationship between classes, enabling a hierarchical classification of objects.

What is Inheritance?

In Java, when one class inherits another class, the inheriting class (subclass or child class) gains access to the non-private members (fields and methods) of the inherited class (superclass or parent class). This mechanism allows for the creation of new classes that are built upon existing classes, extending their functionality without rewriting the code.

Key Concepts

  • Subclass (Child Class): The class that inherits from another class. It can add its own new fields and methods in addition to the inherited ones.
  • Superclass (Parent Class): The class whose features are inherited by another class.
  • extends Keyword: Used to indicate that a class is inheriting from another class (e.g., class Dog extends Animal).
  • super Keyword: Used to refer to the superclass members (methods, fields, and constructors) from within the subclass.

Types of Inheritance Supported by Java

  • Single Inheritance: A class inherits from only one direct superclass.
  • Multilevel Inheritance: A class inherits from a parent class, which in turn inherits from another parent class (e.g., A -> B -> C).
  • Hierarchical Inheritance: Multiple subclasses inherit from a single superclass (e.g., A is inherited by B, C, and D).

Java does not support multiple inheritance for classes (a class cannot extend more than one class directly) to avoid the diamond problem, but it can be achieved indirectly through interfaces.

Example of Inheritance

Let's consider a simple example where a Car class inherits from a Vehicle class. The Vehicle class defines common attributes and behaviors, and the Car class adds specific attributes and behaviors.

Superclass: Vehicle

java
class Vehicle {
    String brand;
    public Vehicle(String brand) {
        this.brand = brand;
    }
    public void honk() {
        System.out.println("Vehicle sound!");
    }
    public void displayBrand() {
        System.out.println("Brand: " + brand);
    }
}

Subclass: Car

java
class Car extends Vehicle {
    String model;
    public Car(String brand, String model) {
        super(brand); // Call the superclass constructor
        this.model = model;
    }
    public void drive() {
        System.out.println(brand + " " + model + " is driving.");
    }
    // Method Overriding
    @Override
    public void honk() {
        System.out.println("Car horn sound!");
    }
}

Demonstration

java
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Toyota", "Camry");
        myCar.displayBrand(); // Inherited from Vehicle
        myCar.honk();         // Overridden method in Car
        myCar.drive();        // Specific to Car

        Vehicle genericVehicle = new Vehicle("Generic");
        genericVehicle.honk(); // Vehicle's honk
    }
}

Advantages of Inheritance

  • Code Reusability: Common code can be placed in a superclass and reused by multiple subclasses.
  • Method Overriding: Allows subclasses to provide specific implementations for methods already defined in their superclass, supporting runtime polymorphism.
  • Eliminates Redundancy: Reduces duplicate code, making the application more efficient and easier to maintain.
  • Maintainability: Changes in the superclass automatically propagate to subclasses, simplifying updates.
  • Extensibility: New functionality can be added to the base class, and all derived classes will inherit it, or new derived classes can be easily created.