🔹 Inheritance in Object-Oriented Programming (OOP)

**Inheritance** is an important concept in Object-Oriented Programming (OOP) that allows a **child class (subclass)** to acquire the properties and behaviors of a **parent class (superclass)**. This promotes **code reusability, extensibility, and organization**.

📌 Why Use Inheritance?

Inheritance helps in **reducing code duplication** and **enhancing maintainability**.

  • ✅ **Code Reusability** – Avoids writing the same code multiple times.
  • ✅ **Improves Code Organization** – Groups similar classes under a parent class.
  • ✅ **Supports Extensibility** – Allows easy modification and extension of existing functionality.
  • ✅ **Enhances Readability** – Simplifies large projects by structuring relationships between classes.

📌 Types of Inheritance

🔹 1. Single Inheritance

A single child class inherits from a single parent class.


            class Animal {
                void eat() {
                    System.out.println("This animal eats food.");
                }
            }
            
            class Dog extends Animal {
                void bark() {
                    System.out.println("Dog barks.");
                }
            }
            
            public class Main {
                public static void main(String[] args) {
                    Dog myDog = new Dog();
                    myDog.eat();  // ✅ Inherited from Animal
                    myDog.bark(); // ✅ Defined in Dog
                }
            }
                

🔹 2. Multilevel Inheritance

A child class inherits from a parent class, which itself is a subclass of another class.


            class Animal {
                void eat() {
                    System.out.println("Animal eats food.");
                }
            }
            
            class Mammal extends Animal {
                void breathe() {
                    System.out.println("Mammals breathe air.");
                }
            }
            
            class Dog extends Mammal {
                void bark() {
                    System.out.println("Dog barks.");
                }
            }
            
            public class Main {
                public static void main(String[] args) {
                    Dog myDog = new Dog();
                    myDog.eat();    // ✅ Inherited from Animal
                    myDog.breathe(); // ✅ Inherited from Mammal
                    myDog.bark();   // ✅ Defined in Dog
                }
            }
                

🔹 3. Hierarchical Inheritance

Multiple child classes inherit from a single parent class.


            class Animal {
                void eat() {
                    System.out.println("Animal eats food.");
                }
            }
            
            class Dog extends Animal {
                void bark() {
                    System.out.println("Dog barks.");
                }
            }
            
            class Cat extends Animal {
                void meow() {
                    System.out.println("Cat meows.");
                }
            }
            
            public class Main {
                public static void main(String[] args) {
                    Dog myDog = new Dog();
                    myDog.eat();  // ✅ Inherited from Animal
                    myDog.bark(); // ✅ Defined in Dog
            
                    Cat myCat = new Cat();
                    myCat.eat();  // ✅ Inherited from Animal
                    myCat.meow(); // ✅ Defined in Cat
                }
            }
                

🔹 4. Multiple Inheritance (Using Interfaces)

Since Java does not support multiple inheritance directly, we use **interfaces**.


            interface Animal {
                void eat();
            }
            
            interface Pet {
                void play();
            }
            
            class Dog implements Animal, Pet {
                public void eat() {
                    System.out.println("Dog eats food.");
                }
            
                public void play() {
                    System.out.println("Dog plays with owner.");
                }
            }
            
            public class Main {
                public static void main(String[] args) {
                    Dog myDog = new Dog();
                    myDog.eat();  // ✅ Implemented from Animal
                    myDog.play(); // ✅ Implemented from Pet
                }
            }
                

📌 Method Overriding in Inheritance

A child class can **override** a method of the parent class to change its behavior.


            class Parent {
                void show() {
                    System.out.println("This is the parent class.");
                }
            }
            
            class Child extends Parent {
                void show() {
                    System.out.println("This is the child class.");
                }
            }
            
            public class Main {
                public static void main(String[] args) {
                    Child obj = new Child();
                    obj.show();  // ✅ Output: "This is the child class."
                }
            }
                

📌 When to Use and Avoid Inheritance

  • ✅ **Use Inheritance** when classes have a **clear "is-a" relationship** (e.g., Dog is an Animal).
  • ✅ **Use Composition** instead of Inheritance when objects should be **flexible and loosely coupled**.
  • ❌ Avoid deep inheritance trees (too many levels) as they make the code **harder to maintain**.

🎯 Summary

**Inheritance** allows classes to share behavior and properties efficiently. It comes in various forms like **Single, Multilevel, Hierarchical, and Multiple Inheritance**. Overriding methods enables **customized behavior** in subclasses.

🔗 Next Topics