Inheritance in OOP 🧬

What is Inheritance? πŸ€”

Inheritance is an OOP principle that allows a class (child/subclass) to derive properties and behaviors from another class (parent/superclass). It promotes code reusability and establishes a hierarchy between classes.

Real-Life Example 🌍:

Think of a family tree πŸ‘¨β€πŸ‘©β€πŸ‘¦. A child inherits traits like eye color or height from their parents, just like a class inherits attributes and methods from another class.


Types of Inheritance πŸ”„

1️⃣ Single Inheritance ➑️

One class inherits from another.

Example πŸ“Œ:

class Animal {
    void makeSound() {
        System.out.println("Some generic animal sound");
    }
}
class Dog extends Animal {
    void bark() {
        System.out.println("Woof! Woof!");
    }
}

2️⃣ Multilevel Inheritance ⏫

A class inherits from a class that is already inherited from another class.

Example πŸ“Œ:

class Animal {
    void eat() { System.out.println("Eating..."); }
}
class Mammal extends Animal {
    void breathe() { System.out.println("Breathing..."); }
}
class Dog extends Mammal {
    void bark() { System.out.println("Barking..."); }
}

3️⃣ Hierarchical Inheritance 🌳

One parent class is inherited by multiple child classes.

Example πŸ“Œ:

class Animal {
    void makeSound() {
        System.out.println("Animal sound");
    }
}
class Dog extends Animal {
    void bark() {
        System.out.println("Bark!");
    }
}
class Cat extends Animal {
    void meow() {
        System.out.println("Meow!");
    }
}

4️⃣ Multiple Inheritance (via Interfaces) 🎭

Java does not support multiple inheritance directly but allows it using interfaces.

Example πŸ“Œ:

interface Engine {
    void start();
}
interface Transmission {
    void shiftGears();
}
class Car implements Engine, Transmission {
    public void start() {
        System.out.println("Car engine starting...");
    }
    public void shiftGears() {
        System.out.println("Car shifting gears...");
    }
}

5️⃣ Hybrid Inheritance πŸ—οΈ

A mix of multiple inheritance types (achieved via interfaces in Java).


super Keyword πŸ†š

The super keyword is used to call the parent class constructor or access parent methods.

Example πŸ“Œ:

class Animal {
    String name = "Animal";
}
class Dog extends Animal {
    String name = "Dog";
    void printName() {
        System.out.println("Parent: " + super.name);
        System.out.println("Child: " + this.name);
    }
}

Why Use Inheritance? πŸ€”

βœ… Code Reusability: Avoids code duplication.
βœ… Hierarchy Representation: Establishes relationships between classes.
βœ… Scalability: New features can be added without modifying existing code.


Conclusion 🎯

Inheritance is a powerful OOP concept that enhances code reusability and organization. It helps build flexible and scalable software applications! πŸš€


πŸ’¬ Which type of inheritance have you used in your projects? Share your thoughts below!

1
Subscribe to my newsletter

Read articles from 𝔏𝔬𝔳𝔦𝔰π”₯ π”Šπ”¬π”Άπ”žπ”© directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

𝔏𝔬𝔳𝔦𝔰π”₯ π”Šπ”¬π”Άπ”žπ”©
𝔏𝔬𝔳𝔦𝔰π”₯ π”Šπ”¬π”Άπ”žπ”©