PYnative

Practice Coding

  • Python â–¼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises â–¼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQsâ–¼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilersâ–¼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Python Object-Oriented Programming (OOP) » Inheritance in Python

Inheritance in Python

Updated on: December 4, 2026 | 11 Comments

The process of inheriting the properties of a parent class into a child class is called inheritance. The existing class is called a base class or parent class, and the new class is called a subclass, child class, or derived class.

In this Python lesson, you will learn inheritance, the types of inheritance, the super() function, method overriding, method overloading, and MRO (Method Resolution Order).

In object-oriented programming, inheritance is an important concept. The main purpose of inheritance is code reusability: we can use an existing class to create a new class instead of writing it from scratch.

In inheritance, the child class acquires all the data members, properties, and methods of the parent class. A child class can also provide its own implementation of the parent class’s methods.

For example, in the real world, a Car is a type of Vehicle. We can create a Car class by inheriting the properties of a Vehicle class, such as wheels, color, fuel tank, and engine, and then add extra properties specific to a car. This is called an “is-a” relationship: a Car is a Vehicle.

Syntax

class BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived classCode language: Python (python)

Also, See

  • Python OOP Exercise

Table of contents

  • Types Of Inheritance
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
  • Python super() function
    • Calling the Parent Constructor with super()
  • issubclass() 
  • Method Overriding
  • Method Overloading
  • Method Resolution Order in Python
  • Summary
  • Next Steps

Types Of Inheritance

In Python, based on the number of child and parent classes involved, there are five types of inheritance:

  1. Single inheritance
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance

Now let’s see each type in detail with an example.

Single Inheritance

In single inheritance, a child class inherits from a single parent class. There is one child class and one parent class.

Python Single Inheritance
Python Single Inheritance

Example

Let’s create one parent class called Vehicle and one child class called Car to implement single inheritance.

# Base class
class Vehicle:
    def vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's method using car object
car.vehicle_info()
car.car_info()
Code language: Python (python)

Output

Inside Vehicle class
Inside Car class

The Car class doesn’t define vehicle_info(), but the car object can still call it because Car inherits it from Vehicle.

Multiple Inheritance

In multiple inheritance, one child class inherits from more than one parent class. There is one child class and multiple parent classes.

Python Multiple Inheritance
Python Multiple Inheritance

Example

# Parent class 1
class Person:
    def person_info(self, name, age):
        print('Inside Person class')
        print('Name:', name, 'Age:', age)

# Parent class 2
class Company:
    def company_info(self, company_name, location):
        print('Inside Company class')
        print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):
    def employee_info(self, salary, skill):
        print('Inside Employee class')
        print('Salary:', salary, 'Skill:', skill)

# Create object of Employee
emp = Employee()

# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.employee_info(12000, 'Machine Learning')
Code language: Python (python)

Output

Inside Person class
Name: Jessa Age: 28
Inside Company class
Name: Google location: Atlanta
Inside Employee class
Salary: 12000 Skill: Machine Learning

In the above example, we created two parent classes, Person and Company. Then we created one child class, Employee, which inherits from both Person and Company. So an Employee object can use the methods of both parents.

Multilevel inheritance

In multilevel inheritance, a class inherits from a child class (a derived class). Suppose there are three classes A, B, and C. A is the superclass, B is the child class of A, and C is the child class of B. In other words, a chain of classes is called multilevel inheritance.

Python Multilevel Inheritance
Python Multilevel Inheritance

Example

# Base class
class Vehicle:
    def vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Child class
class SportsCar(Car):
    def sports_car_info(self):
        print('Inside SportsCar class')

# Create object of SportsCar
s_car = SportsCar()

# access Vehicle's and Car's methods using SportsCar object
s_car.vehicle_info()
s_car.car_info()
s_car.sports_car_info()
Code language: Python (python)

Output

Inside Vehicle class
Inside Car class
Inside SportsCar class

In the above example, there are three classes: Vehicle, Car, and SportsCar. Vehicle is the superclass, Car is a child of Vehicle, and SportsCar is a child of Car. This is the chaining of classes.

Hierarchical Inheritance

In hierarchical inheritance, more than one child class is derived from a single parent class. In other words, there is one parent class and multiple child classes.

Python hierarchical inheritance
Python hierarchical inheritance

Example

Let’s create Vehicle as a parent class and two child classes, Car and Truck.

class Vehicle:
    def info(self):
        print("This is Vehicle")

class Car(Vehicle):
    def car_info(self, name):
        print("Car name is:", name)

class Truck(Vehicle):
    def truck_info(self, name):
        print("Truck name is:", name)

obj1 = Car()
obj1.info()
obj1.car_info('BMW')

obj2 = Truck()
obj2.info()
obj2.truck_info('Ford')Code language: Python (python)

Output

This is Vehicle
Car name is: BMW
This is Vehicle
Truck name is: Ford

Hybrid Inheritance

When inheritance combines two or more of the above types, it is called hybrid inheritance.

Python hybrid inheritance
Python hybrid inheritance

Example

class Vehicle:
    def vehicle_info(self):
        print("Inside Vehicle class")

class Car(Vehicle):
    def car_info(self):
        print("Inside Car class")

class Truck(Vehicle):
    def truck_info(self):
        print("Inside Truck class")

# SportsCar inherits from both Car and Vehicle
class SportsCar(Car, Vehicle):
    def sports_car_info(self):
        print("Inside SportsCar class")

# create object
s_car = SportsCar()

s_car.vehicle_info()
s_car.car_info()
s_car.sports_car_info()Code language: Python (python)

Output

Inside Vehicle class
Inside Car class
Inside SportsCar class

Note: The above example combines hierarchical and multiple inheritance. The parent class Vehicle has two child classes, Car and Truck. This is hierarchical inheritance.

In addition, SportsCar inherits from two parent classes, Car and Vehicle. This is multiple inheritance.

Python super() function

In a child class, we can refer to the parent class by using the super() function. The super() function returns a temporary object of the parent class that allows us to call the parent class’s methods inside a child class method.

Benefits of using the super() function:

  1. We don’t need to remember or hard-code the parent class name to access its methods.
  2. We can use the super() function in both single and multiple inheritance.
  3. The super() function supports code reusability, because we can extend a parent method instead of rewriting it.

Example

class Company:
    def company_name(self):
        return 'Google'

class Employee(Company):
    def info(self):
        # Calling the superclass method using super() function
        c_name = super().company_name()
        print("Jessa works at", c_name)

# Creating object of child class
emp = Employee()
emp.info()Code language: Python (python)

Output:

Jessa works at Google

In the above example, we create a parent class Company and a child class Employee. In the Employee class, we call the parent class method using the super() function.

Calling the Parent Constructor with super()

The most common use of super() is to call the parent class’s constructor (__init__()) from the child class constructor. This way, the parent initializes its own attributes, and the child only adds what is new.

Note: If a child class defines its own __init__(), the parent’s __init__() is not called automatically. You must call it explicitly with super().__init__().

class Vehicle:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

class Car(Vehicle):
    def __init__(self, brand, color, seats):
        # call the parent class constructor
        super().__init__(brand, color)
        self.seats = seats

    def show(self):
        print('Brand:', self.brand, '| Color:', self.color, '| Seats:', self.seats)

car = Car('BMW', 'Black', 5)
car.show()Code language: Python (python)

Output:

Brand: BMW | Color: Black | Seats: 5

issubclass() 

In Python, we can check whether a particular class is a subclass of another class using the built-in function issubclass(). This function returns True if the given class is a subclass of the specified class. Otherwise, it returns False.

Syntax

issubclass(class, classinfo)Code language: Python (python)

Where,

  • class: The class to be checked.
  • classinfo: A class, a type, or a tuple of classes and types. If it is a tuple, the function returns True when the class is a subclass of any of them.

Example

class Company:
    def fun1(self):
        print("Inside parent class")

class Employee(Company):
    def fun2(self):
        print("Inside child class.")

class Player:
    def fun3(self):
        print("Inside Player class.")

# Result True
print(issubclass(Employee, Company))

# Result False
print(issubclass(Employee, list))

# Result False
print(issubclass(Player, Company))

# Result True
print(issubclass(Employee, (list, Company)))

# Result True
print(issubclass(Company, (list, Company)))Code language: Python (python)

Output:

True
False
False
True
True

Note: A class is considered a subclass of itself, which is why the last line returns True. To check whether an object (not a class) belongs to a class, use Python isinstance().

Method Overriding

In inheritance, all members of the parent class are available in the child class by default. If the parent class’s implementation of a method doesn’t suit the child class, the child class can redefine that method with its own behavior. This concept is called method overriding.

When a child class defines a method with the same name as a method in its superclass, the child’s method is said to override the parent’s method. When you call that method on a child object, Python runs the child’s version.

Python method overriding
Python method overriding

Example

class Vehicle:
    def max_speed(self):
        print("max speed is 100 Km/Hour")

class Car(Vehicle):
    # overriding the implementation of Vehicle class
    def max_speed(self):
        print("max speed is 200 Km/Hour")

# Creating object of Car class
car = Car()
car.max_speed()Code language: Python (python)

Output:

max speed is 200 Km/Hour

In the above example, we create two classes: Vehicle (parent class) and Car (child class). Car extends Vehicle, so all properties of the parent class are available in the child class. In addition, the child class redefines the method max_speed().

Extending instead of replacing: An overriding method can still run the parent’s version by calling it with super(), and then add its own behavior.

class Vehicle:
    def max_speed(self):
        print("Max speed is 100 km/h")

class Car(Vehicle):
    def max_speed(self):
        # call the parent version first
        super().max_speed()
        print("Car max speed is 200 km/h")

car = Car()
car.max_speed()Code language: Python (python)

Output:

Max speed is 100 km/h
Car max speed is 200 km/h

Method Overloading

Method overloading means defining multiple methods with the same name but different parameters. Languages like Java and C++ support it directly, but Python does not. If you define two methods with the same name in a class, the second definition simply replaces the first.

class Calculator:
    def add(self, a, b):
        return a + b

    # this replaces the add() method above
    def add(self, a, b, c):
        return a + b + c

calc = Calculator()
print(calc.add(1, 2, 3))  # 6
print(calc.add(1, 2))     # TypeErrorCode language: Python (python)

Output:

6
TypeError: Calculator.add() missing 1 required positional argument: 'c'

Instead, Python achieves the same result with default arguments or variable-length arguments (*args), so a single method can handle different numbers of arguments.

class Calculator:
    # default arguments make b and c optional
    def add(self, a, b=0, c=0):
        return a + b + c

calc = Calculator()
print(calc.add(5))
print(calc.add(5, 10))
print(calc.add(5, 10, 15))

class Shape:
    # *sides accepts any number of arguments
    def area(self, *sides):
        if len(sides) == 1:
            return sides[0] * sides[0]      # square
        elif len(sides) == 2:
            return sides[0] * sides[1]      # rectangle
        return 0

s = Shape()
print(s.area(4))
print(s.area(4, 6))Code language: Python (python)

Output:

5
15
30
16
24

Method Resolution Order in Python

In Python, the Method Resolution Order (MRO) is the order in which Python looks for a method or attribute. Python first searches the class itself, then its parent classes in the order we listed them while inheriting.

This order is also called the linearization of a class, and the set of rules that produces it is called the MRO. The MRO plays an essential role in multiple inheritance, because the same method may be found in multiple parent classes.

In multiple inheritance, Python follows this search order:

  1. First, it searches the current class. If the method is not found, it searches the parent classes in the order they were specified while inheriting (left to right).
  2. A parent class is searched only after all of its child classes have been searched. Python computes this order using the C3 linearization algorithm.
  3. To see the MRO of a class, use either the __mro__ attribute or the mro() method.

Example

class A:
    def process(self):
        print("In class A")

class B(A):
    def process(self):
        print("In class B")

class C(B, A):
    def process(self):
        print("In class C")

# Creating object of C class
c1 = C()
c1.process()
print(C.mro())Code language: Python (python)

Output:

In class C
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]

In the above example, we create three classes: A, B, and C. Class B inherits from A, and class C inherits from B and A. When we create an object of class C and call the process() method, Python looks for process() in class C itself first.

If it’s not found there, Python searches the parent classes B and then A, because C is defined as C(B, A). The search always goes from left to right. Every class ultimately inherits from Python’s built-in object class, which is why it appears last.

The diamond problem: In the following example, D inherits from B and C, and both inherit from A. The MRO makes sure A is searched only once, after both B and C.

class A:
    def show(self):
        print("In class A")

class B(A):
    def show(self):
        print("In class B")

class C(A):
    def show(self):
        print("In class C")

class D(B, C):
    pass

d = D()
d.show()
print([cls.__name__ for cls in D.__mro__])Code language: Python (python)

Output:

In class B
['D', 'B', 'C', 'A', 'object']

Summary

  • Inheritance lets a child class reuse the attributes and methods of a parent class.
  • Python supports single, multiple, multilevel, hierarchical, and hybrid inheritance.
  • Use super() to call parent methods, especially the parent’s __init__().
  • Use issubclass() to check class relationships and isinstance() to check objects.
  • Method overriding lets a child class replace or extend a parent method.
  • Python doesn’t support classic method overloading. Use default arguments or *args instead.
  • The MRO decides which method runs when several classes define it. Check it with ClassName.mro().

Next Steps

  • Python OOP Exercise
  • Polymorphism in Python
  • Encapsulation in Python

Filed Under: Python, Python Object-Oriented Programming (OOP)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Object-Oriented Programming (OOP)

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes
In: Python Python Object-Oriented Programming (OOP)
TweetF  sharein  shareP  Pin

  Python OOP

  • Python OOP
  • Classes and Objects in Python
  • Constructors in Python
  • Python Destructors
  • Encapsulation in Python
  • Polymorphism in Python
  • Inheritance in Python
  • Python Instance Variables
  • Python Instance Methods
  • Python Class Variables
  • Python Class Method
  • Python Static Method
  • Python Class Method vs. Static Method vs. Instance Method
  • Python OOP exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com