Inheritance in Java

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors (fields and methods) from another class. It promotes code reuse and establishes a natural hierarchical relationship between classes.

Key Concepts of Inheritance

  1. Superclass (Parent Class) and Subclass (Child Class):
  2. extends Keyword:
  3. Single Inheritance:
  4. super Keyword:

Types of Inheritance in Java

  1. Single Inheritance:

    class Animal {
        void eat() {
            System.out.println("This animal eats food.");
        }
    }
    
    class Dog extends Animal {
        void bark() {
            System.out.println("The dog barks.");
        }
    }
    
    
  2. Multilevel Inheritance:

    class Animal {
        void eat() {
            System.out.println("This animal eats food.");
        }
    }
    
    class Dog extends Animal {
        void bark() {
            System.out.println("The dog barks.");
        }
    }
    
    class Labrador extends Dog {
        void display() {
            System.out.println("This is a Labrador.");
        }
    }
    
    
  3. Hierarchical Inheritance:

    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.");
        }
    }
    
    
  4. Hybrid Inheritance (Through Interfaces):

    interface Animal {
        void eat();
    }
    
    interface Pet {
        void play();
    }
    
    class Dog implements Animal, Pet {
        public void eat() {
            System.out.println("This dog eats food.");
        }
    
        public void play() {
            System.out.println("The dog plays.");
        }
    }
    
    

Diagram Representation

Here’s a UML diagram representing the types of inheritance in Java:

Single Inheritance:
Animal
  |
 Dog

Multilevel Inheritance:
Animal
  |
 Dog
  |
Labrador

Hierarchical Inheritance:
    Animal
   /     \\\\
 Dog     Cat

Hybrid Inheritance:
   Animal        Pet
      \\\\          /
       \\\\        /
        \\\\      /
         \\\\    /
          Dog

Detailed Example

Let's illustrate inheritance with a more detailed example:

// Superclass
class Animal {
    String name;

    Animal(String name) {
        this.name = name;
    }

    void eat() {
        System.out.println(name + " eats.");
    }

    void sleep() {
        System.out.println(name + " sleeps.");
    }
}

// Subclass
class Dog extends Animal {
    String breed;

    Dog(String name, String breed) {
        super(name); // Calling the superclass constructor
        this.breed = breed;
    }

    void bark() {
        System.out.println(name + " barks.");
    }

    // Overriding the eat method
    @Override
    void eat() {
        System.out.println(name + " eats dog food.");
    }
}

// Another Subclass
class Cat extends Animal {
    boolean isIndoor;

    Cat(String name, boolean isIndoor) {
        super(name);
        this.isIndoor = isIndoor;
    }

    void meow() {
        System.out.println(name + " meows.");
    }

    // Overriding the sleep method
    @Override
    void sleep() {
        System.out.println(name + " sleeps on a cushion.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy", "Golden Retriever");
        dog.eat(); // Buddy eats dog food.
        dog.sleep(); // Buddy sleeps.
        dog.bark(); // Buddy barks.

        Cat cat = new Cat("Whiskers", true);
        cat.eat(); // Whiskers eats.
        cat.sleep(); // Whiskers sleeps on a cushion.
        cat.meow(); // Whiskers meows.
    }
}

In this example: