Inheritance in Java with Example

Introduction

Inheritance is one of the four fundamental principles of Object-Oriented Programming (OOP). It allows a class to inherit properties and behaviors (fields and methods) from another class. The class that inherits the properties is called the subclass (or derived class), and the class from which properties are inherited is called the superclass (or base class). Inheritance promotes code reusability and establishes a natural hierarchical relationship between classes.

Table of Contents

  1. What is Inheritance?
  2. Benefits of Inheritance
  3. Types of Inheritance
  4. Single Inheritance
  5. Multilevel Inheritance
  6. Hierarchical Inheritance
  7. Real-World Examples of Inheritance
  8. Example: Single Inheritance
  9. Example: Multilevel Inheritance
  10. Example: Hierarchical Inheritance
  11. Conclusion

1. What is Inheritance?

Inheritance is a mechanism wherein a new class is derived from an existing class. The derived class (child class) inherits the attributes and methods of the base class (parent class), allowing code reuse and the creation of a natural hierarchy.

2. Benefits of Inheritance

  • Code Reusability: Inheritance allows a class to reuse the fields and methods of another class.
  • Method Overriding: Subclasses can provide specific implementations for methods that are defined in the parent class.
  • Polymorphism: Inheritance supports polymorphism, allowing objects to be treated as instances of their parent class.

3. Types of Inheritance

  • Single Inheritance: A class inherits from one superclass.
  • Multilevel Inheritance: A class inherits from a superclass, and another class inherits from this derived class.
  • Hierarchical Inheritance: Multiple classes inherit from a single superclass.
  • Multiple Inheritance: A class inherits from more than one superclass. Java does not support multiple inheritance directly to avoid complexity and ambiguity. However, it can be achieved using interfaces.

4. Single Inheritance

In a single inheritance, a class inherits from one superclass.

Example:

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

5. Multilevel Inheritance

In multilevel inheritance, a class inherits from a superclass, and another class inherits from this derived class.

Example:

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

class Puppy extends Dog {
    void weep() {
        System.out.println("The puppy weeps.");
    }
}

6. Hierarchical Inheritance

In hierarchical inheritance, multiple classes inherit from a single superclass.

Example:

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("The cat meows.");
    }
}

Multiple Inheritance (Through Interfaces)

In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. 

Please note that Java does not support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through Interfaces. 

In the image below, Class C is derived from Class A and Class B.

7. Real-World Examples of Inheritance

Example 1: Vehicle Hierarchy

Consider a vehicle hierarchy where Vehicle is the base class. Car and Bike can be derived classes that inherit properties and methods from Vehicle.

Example 2: Employee Hierarchy

In an employee management system, Employee can be the base class. Manager and Developer can be derived classes that inherit from Employee.

Example 3: Number hierarchy from java.lang library

The Java library extensively uses inheritance. The figure below shows an inheritance hierarchy from java.lang library. The Number class abstracts various numerical (reference) types such as ByteIntegerFloatDoubleShort, and BigDecimal.

8. Example: Single Inheritance

Example:

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // Output: This animal eats food.
        dog.bark(); // Output: The dog barks.
    }
}

Explanation:

  • Animal: Superclass with a method eat.
  • Dog: Subclass that inherits from Animal and adds a method bark.

9. Example: Multilevel Inheritance

Example:

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

class Puppy extends Dog {
    void weep() {
        System.out.println("The puppy weeps.");
    }
}

public class Main {
    public static void main(String[] args) {
        Puppy puppy = new Puppy();
        puppy.eat();  // Output: This animal eats food.
        puppy.bark(); // Output: The dog barks.
        puppy.weep(); // Output: The puppy weeps.
    }
}

Explanation:

  • Animal: Superclass with a method eat.
  • Dog: Subclass that inherits from Animal and adds a method bark.
  • Puppy: Subclass that inherits from Dog and adds a method weep.

10. Example: Hierarchical Inheritance

Example:

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("The cat meows.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // Output: This animal eats food.
        dog.bark(); // Output: The dog barks.

        Cat cat = new Cat();
        cat.eat();  // Output: This animal eats food.
        cat.meow(); // Output: The cat meows.
    }
}

Explanation:

  • Animal: Superclass with a method eat.
  • Dog and Cat: Subclasses that inherit from Animal and add methods bark and meow, respectively.

11. Conclusion

Inheritance in Java is a powerful concept that promotes code reusability and establishes a natural hierarchical relationship between classes. By using inheritance, you can create a base class with common properties and methods and then create derived classes that inherit these properties and methods while adding specific features. Understanding and applying inheritance helps to build a structured and organized codebase, making it easier to maintain and extend.

Happy coding!

Comments

Post a Comment

Leave Comment