Inheritance in Python

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (known as the child or derived class) to inherit attributes and methods from another class (known as the parent or base class). This promotes code reuse and establishes a relationship between classes.

Types of Inheritance

1. Single Inheritance

In single inheritance, a derived class inherits from one base class.

Code Example

class Animal:
            def speak(self):
                return "Animal speaks"
        
        class Dog(Animal):
            def bark(self):
                return "Dog barks"
        
        dog = Dog()
        print(dog.speak())  # Inherited method
        print(dog.bark())   # Method of Dog class

Output

Animal speaks
Dog barks

2. Multiple Inheritance

In multiple inheritance, a class can inherit from more than one parent class.

Code Example

class Flyer:
            def fly(self):
                return "Can fly"
        
        class Swimmer:
            def swim(self):
                return "Can swim"
        
        class Duck(Flyer, Swimmer):
            def quack(self):
                return "Duck quacks"
        
        duck = Duck()
        print(duck.fly())   # Method from Flyer class
        print(duck.swim())  # Method from Swimmer class
        print(duck.quack()) # Method of Duck class

Output

Can fly
Can swim
Duck quacks

3. Multilevel Inheritance

In multilevel inheritance, a class is derived from another derived class.

Code Example

class Grandparent:
            def traits(self):
                return "Has good traits"
        
        class Parent(Grandparent):
            def job(self):
                return "Has a job"
        
        class Child(Parent):
            def play(self):
                return "Child plays"
        
        child = Child()
        print(child.traits())  # Inherited from Grandparent
        print(child.job())     # Inherited from Parent
        print(child.play())    # Method of Child class

Output

Has good traits
Has a job
Child plays

4. Hierarchical Inheritance

In hierarchical inheritance, multiple classes inherit from a single base class.

Code Example

class Vehicle:
            def start(self):
                return "Vehicle starts"
        
        class Car(Vehicle):
            def drive(self):
                return "Car drives"
        
        class Bike(Vehicle):
            def ride(self):
                return "Bike rides"
        
        car = Car()
        bike = Bike()
        print(car.start())  # Inherited method
        print(car.drive())  # Method of Car class
        print(bike.start()) # Inherited method
        print(bike.ride())  # Method of Bike class

Output

Vehicle starts
Car drives
Vehicle starts
Bike rides

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance.

Code Example

class Animal:
            def eat(self):
                return "Eating"
        
        class Bird(Animal):
            def fly(self):
                return "Flying"
        
        class Fish(Animal):
            def swim(self):
                return "Swimming"
        
        class Duck(Bird, Fish):
            def quack(self):
                return "Quacking"
        
        duck = Duck()
        print(duck.eat())   # Inherited from Animal
        print(duck.fly())   # Inherited from Bird
        print(duck.swim())  # Inherited from Fish
        print(duck.quack()) # Method of Duck class

Output

Eating
Flying
Swimming
Quacking

Flowchart Of Inheritance

classDiagram Animal <|-- Mammal Animal <|-- Bird Mammal <|-- Dog Mammal <|-- Cat Bird <|-- Sparrow Bird <|-- Eagle class Animal { +name: str +age: int +eat() +sleep() } class Mammal { +has_fur: bool +walk() } class Bird { +has_feathers: bool +fly() } class Dog { +bark() } class Cat { +meow() } class Sparrow { +chirp() } class Eagle { +hunt() }

Conclusion

Inheritance in Python provides a powerful way to establish relationships between classes, allowing for code reuse and organization. By using different types of inheritance, developers can design flexible and modular applications.