Inheritance in Python

Inheritance in Python is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and behaviors (methods) of another class. Python, being an object-oriented language, supports inheritance, making it easier to write reusable and maintainable code.

In this blog post, we will dive deep into inheritance in Python — what it is, how it works, its types, and real-world examples.

Want to learn about Graphs in Data Structure Let’s Go

What is Inheritance?

Inheritance in Python is the process of creating a new class, known as the derived class, that inherits attributes and methods from an existing class, known as the base class

Inheritance in Python

Basic Syntax of Inheritance

class ParentClass:
# Parent class code
class ChildClass(ParentClass):
    # Child class code

The child class inherits everything from the parent class and can add its own features or override the parent’s features.

Example of Inheritance in Python

Parent Class

class Animal:
    def speak(self):
        print("Animal speaks")

Child Class

class Dog(Animal):
    def speak(self):
        print("Dog barks")

Usage

dog = Dog()
dog.speak() # Output: Dog barks

Here, the Dog class inherits from Animal but overrides the speak() method.

Types of Inheritance in Python

Single Inheritance

Single inheritance in Python is a type of inheritance where a class (the child or derived class) inherits attributes and methods from only one other class (the parent or base class).

Single
class Parent:
    def show(self):
        print("This is Parent")

class Child(Parent):
    pass

obj = Child()
obj.show() 

Output:

This is Parent

Multiple Inheritance

When a class is derived from more than one base class it is called Multiple Inheritance. The derived class inherits all the features of the base case.

Multiple

Code



class Father:
    def skills(self):
        print("Father skills: Driving")

class Mother:
    def skills(self):
        print("Mother skills: Cooking")

class Child(Father, Mother):
    def skills(self):
        Father.skills(self)
        Mother.skills(self)
        print("Child skills: Coding")

c = Child()
c.skills()

Output:

Father skills: Driving
Mother skills: Cooking
Child skills: Coding

Multilevel Inheritance

Multilevel Inheritance in Python is a type of Inheritance that involves inheriting a class that has already inherited some other class.

Multilevel

Code

 class Grandparent:
    def house(self):
        print("Grandparent's house")

class Parent(Grandparent):
    def car(self):
        print("Parent's car")

class Child(Parent):
    def bike(self):
        print("Child's bike")

c = Child()
c.house()
c.car()
c.bike()

Output:

Grandparent's house
Parent's car
Child's bike

Hierarchical Inheritance

Hierarchical inheritance is a type of inheritance in which multiple classes inherit from a single superclass.

Hierarchical

Code

class Parent:
    def show(self):
        print("Parent method")

class Child1(Parent):
    pass

class Child2(Parent):
    pass

c1 = Child1()
c2 = Child2()

c1.show()
c2.show()

Output:

Parent method
Parent method

Hybrid Inheritance

Hybrid inheritance in Python is a combination of multiple and hierarchical inheritance.

Hybrid

Example combining multiple and hierarchical inheritance:

class A:
    def featureA(self):
         print("Feature A")

class B(A):
    def featureB(self):
        print("Feature B")

class C(A):
    def featureC(self):
        print("Feature C")

class D(B, C):
    def featureD(self):
        print("Feature D")

d = D()
d.featureA()
d.featureB()
d.featureC()
d.featureD()

Output:

Feature A
Feature B
Feature C
Feature D

Method Resolution Order (MRO)

In multiple inheritance in Python follows the Method Resolution Order (MRO) to decide which class method is called when there are multiple parent classes.

Check MRO using:

print(D.mro())

or

help(D)

super() Function in Python

The super() function is used to call the methods of the parent class.

Example:

class Animal:
    def init(self, name):
        self.name = name

def speak(self):
    print(f"{self.name} makes a sound")

class Dog(Animal):
    def speak(self):
        super().speak() # Calls parent class method
        print(f"{self.name} barks")

dog = Dog("Tommy")
dog.speak()

Output:

Tommy makes a sound
Tommy barks

Benefits of Inheritance

  • Code reusability
  • Reduces redundancy
  • Improves code organization
  • Easier maintenance and scalability

When to Avoid Inheritance

  • When classes are unrelated
  • When it creates deep or complicated hierarchies
  • Prefer composition over inheritance when possible (i.e., use objects within objects rather than relying heavily on inheritance)

Conclusion

Inheritance is a powerful feature in Python that enables developers to write clean, efficient, and reusable code. Understanding the different types of inheritance and how to use them properly can greatly improve the structure and maintainability of your programs.

FAQ:

Types of inheritance

Multiple inheritance

Hybrid inheritance

Multilevel inheritance

Hierarchical inheritance

Single inheritance

1 thought on “Inheritance in Python”

  1. Pingback: Polymorphism in Python

Leave a Comment

Your email address will not be published. Required fields are marked *

Index