Lesson 52 of 70 – Python Inheritance
74%

Python Inheritance

Inheritance is an important concept of Object Oriented Programming (OOP). It allows one class to acquire attributes and methods from another class. Inheritance helps us reuse existing code and create relationships between classes.

Note: The class that provides properties and methods is called the parent class, while the class that inherits them is called the child class.

What is Inheritance?

Inheritance allows a child class to reuse attributes and methods defined in a parent class.

class Animal:

    def speak(self):

        print("Animal makes a sound")


class Dog(Animal):

    pass


dog = Dog()

dog.speak()

Output:

Animal makes a sound

Parent Class and Child Class

The class being inherited from is called the parent class, base class, or superclass. The class that inherits from it is called the child class, derived class, or subclass.

class Person:

    def show_person(self):

        print("I am a person")


class Student(Person):

    pass

Inheritance Syntax

To inherit from another class, place the parent class inside parentheses after the child class name.

class Parent:

    pass


class Child(Parent):

    pass

Single Inheritance

When one child class inherits from one parent class, it is called single inheritance.

class Animal:

    def eat(self):

        print("Animal eats")


class Dog(Animal):

    def bark(self):

        print("Dog barks")


dog = Dog()

dog.eat()
dog.bark()

Output:

Animal eats
Dog barks

Using an Inherited Method

A child object can directly call a method inherited from its parent.

class Vehicle:

    def start(self):

        print("Vehicle started")


class Car(Vehicle):

    pass


car = Car()

car.start()

Output:

Vehicle started

Adding New Methods in Child Class

A child class can add its own methods in addition to inherited methods.

class Animal:

    def eat(self):

        print("Eating")


class Dog(Animal):

    def bark(self):

        print("Barking")


dog = Dog()

dog.eat()
dog.bark()

Output:

Eating
Barking

Inheritance and Constructor

If a child class does not define its own __init__() method, it can inherit the parent's initializer.

class Person:

    def __init__(self, name):

        self.name = name


class Student(Person):

    pass


student = Student("Rahul")

print(student.name)

Output:

Rahul

Child Class Constructor

If the child class defines its own __init__(), it replaces the inherited initializer for that child class. The parent initializer can be called explicitly when needed.

class Person:

    def __init__(self, name):

        self.name = name


class Student(Person):

    def __init__(self, name, course):

        self.name = name
        self.course = course


student = Student(
    "Rahul",
    "Python"
)

print(student.name)
print(student.course)

Output:

Rahul
Python

The super() Function

The super() function provides access to methods and behavior from a parent class. It is commonly used to call the parent's initializer.

class Person:

    def __init__(self, name):

        self.name = name


class Student(Person):

    def __init__(self, name, course):

        super().__init__(name)

        self.course = course


student = Student(
    "Rahul",
    "Python"
)

print(student.name)
print(student.course)

Output:

Rahul
Python

Method Overriding

A child class can define a method with the same name as a method in the parent class. The child implementation is then used when the method is called on a child object.

class Animal:

    def sound(self):

        print("Animal sound")


class Dog(Animal):

    def sound(self):

        print("Bark")


dog = Dog()

dog.sound()

Output:

Bark

Calling Parent Method with super()

When a child overrides a method, super() can be used to call the parent implementation as part of the child implementation.

class Animal:

    def sound(self):

        print("Animal sound")


class Dog(Animal):

    def sound(self):

        super().sound()

        print("Bark")


dog = Dog()

dog.sound()

Output:

Animal sound
Bark

Multilevel Inheritance

When a class inherits from another child class, it creates a chain of inheritance. This is called multilevel inheritance.

class Grandparent:

    def show_grandparent(self):

        print("Grandparent")


class Parent(Grandparent):

    def show_parent(self):

        print("Parent")


class Child(Parent):

    def show_child(self):

        print("Child")


obj = Child()

obj.show_grandparent()
obj.show_parent()
obj.show_child()

Output:

Grandparent
Parent
Child

Multiple Inheritance

When one child class inherits from more than one parent class, it is called multiple inheritance.

class Father:

    def skill1(self):

        print("Driving")


class Mother:

    def skill2(self):

        print("Cooking")


class Child(Father, Mother):

    pass


child = Child()

child.skill1()
child.skill2()

Output:

Driving
Cooking

Hierarchical Inheritance

When multiple child classes inherit from the same parent class, it is called hierarchical inheritance.

class Animal:

    def eat(self):

        print("Eating")


class Dog(Animal):

    def bark(self):

        print("Barking")


class Cat(Animal):

    def meow(self):

        print("Meowing")


dog = Dog()

cat = Cat()

dog.eat()
dog.bark()

cat.eat()
cat.meow()

Output:

Eating
Barking
Eating
Meowing

Hybrid Inheritance

Hybrid inheritance is a combination of two or more inheritance patterns. Python supports complex inheritance structures.

class A:

    def show_a(self):

        print("A")


class B(A):

    def show_b(self):

        print("B")


class C(A):

    def show_c(self):

        print("C")


class D(B, C):

    pass


obj = D()

obj.show_a()
obj.show_b()
obj.show_c()

Output:

A
B
C

Method Resolution Order (MRO)

Python uses Method Resolution Order (MRO) to determine the order in which classes are searched for attributes and methods. This is especially important with multiple inheritance.

class A:

    pass


class B(A):

    pass


class C(A):

    pass


class D(B, C):

    pass


print(D.mro())

The result shows the order in which Python searches the classes.

MRO Example

class A:

    def show(self):

        print("A")


class B(A):

    def show(self):

        print("B")


class C(A):

    def show(self):

        print("C")


class D(B, C):

    pass


obj = D()

obj.show()

Output:

B

Python follows the MRO and finds the method in B before continuing to C.

Using issubclass()

The issubclass() function checks whether a class is a subclass of another class. It returns True or False.

class Animal:

    pass


class Dog(Animal):

    pass


print(issubclass(Dog, Animal))

Output:

True

Inheritance with isinstance()

An object of a child class is also considered an instance of its parent class.

class Animal:

    pass


class Dog(Animal):

    pass


dog = Dog()

print(isinstance(dog, Dog))

print(isinstance(dog, Animal))

Output:

True
True

Advantages of Inheritance

  • Promotes code reuse.
  • Reduces duplicate code.
  • Allows child classes to extend parent classes.
  • Supports method overriding.
  • Helps model relationships between classes.
  • Works with polymorphism.
  • Makes large programs easier to organize.

When to Use Inheritance?

Inheritance is useful when there is a clear "is-a" relationship between classes.

Dog is an Animal

Car is a Vehicle

Student is a Person

If the relationship is instead "has-a", composition is often a better design choice.

Car has an Engine

Student has an Address

Complete Inheritance Example

class Person:

    def __init__(self, name, age):

        self.name = name
        self.age = age


    def display_person(self):

        print("Name:", self.name)
        print("Age:", self.age)


class Student(Person):

    def __init__(
        self,
        name,
        age,
        course
    ):

        super().__init__(name, age)

        self.course = course


    def display_student(self):

        self.display_person()

        print("Course:", self.course)


student = Student(
    "Rahul",
    20,
    "Python"
)

student.display_student()

Output:

Name: Rahul
Age: 20
Course: Python

Types of Inheritance

Type Description
Single One child inherits from one parent.
Multiple One child inherits from multiple parents.
Multilevel Inheritance occurs through multiple levels.
Hierarchical Multiple children inherit from one parent.
Hybrid Combination of multiple inheritance patterns.

Key Points

  • Inheritance allows one class to reuse another class's behavior.
  • The inherited class is called the parent or base class.
  • The inheriting class is called the child or derived class.
  • Python supports single, multiple, multilevel, hierarchical, and hybrid inheritance patterns.
  • A child class can add its own methods and attributes.
  • A child class can override inherited methods.
  • super() can be used to access parent-class behavior.
  • issubclass() checks the relationship between classes.
  • isinstance() can recognize instances of a parent class as well as the child class.
  • Python uses Method Resolution Order (MRO) when resolving inherited attributes and methods.
  • Inheritance promotes code reuse and extensibility.

🧠 Quick Quiz

Question: Which function is commonly used to call a parent class method or initializer?