Inheritance is one of the most important concepts in Object-Oriented Programming (OOP) in Python. It allows a class to acquire attributes and methods from another class, helping developers reuse existing code instead of writing the same functionality again. The class that provides the attributes and methods is called the parent class, base class, or superclass, while the class that inherits them is called the child class, derived class, or subclass. For example, a Vehicle class can contain common functionality such as brand, start(), and stop(), which can then be inherited by classes such as Car and Bike. Inheritance makes programs more organized, reusable, and easier to maintain.
Table of Contents
What is Inheritance in Python?
Inheritance is a feature of Object-Oriented Programming that allows one class to inherit attributes and methods from another class. The existing class is called the parent class, while the new class that inherits from it is called the child class.
The child class can use the accessible methods and attributes of the parent class and can also define its own additional methods and attributes.
Example:
# Python program to demonstrate inheritance
class Animal:
def eat(self):
print("Animal is eating")
class Dog(Animal):
def bark(self):
print("Dog is barking")
dog1 = Dog()
dog1.eat()
dog1.bark()
Output:
Animal is eating
Dog is barking
Explanation:
- The Animal class is created with an eat() method.
- The Dog class inherits from the Animal class.
- The Dog class has its own bark() method.
- A Dog object named dog1 is created.
- The dog1 object can use the inherited eat() method.
- It can also use its own bark() method.
This demonstrates how inheritance allows a child class to reuse functionality from its parent class.
Why Use Inheritance in Python?
Inheritance is useful when multiple classes share common properties or behavior.
- Code Reusability: Inheritance allows a child class to reuse methods and attributes from a parent class without rewriting the same code.
- Reduces Code Duplication: Common functionality can be placed in a parent class and reused by multiple child classes.
- Improves Code Organization: Related classes can be organized into a clear hierarchy.
- Makes Programs Easier to Maintain: Common functionality can be managed in the parent class and reused by child classes.
- Supports Method Overriding: A child class can provide its own implementation of a method inherited from the parent class.
- Supports Polymorphism: Different child classes can provide different implementations of common methods.
- Represents Real-World Relationships: Inheritance can represent relationships such as Car is a Vehicle or Dog is an Animal.
How Inheritance Works in Python?
Inheritance is created by placing the parent class name inside parentheses after the child class name. When a child class inherits from a parent class, it can access the parent's accessible attributes and methods.
Example:
# Python program to demonstrate how inheritance works
class Person:
def introduce(self):
print("I am a person")
class Student(Person):
def study(self):
print("I am studying")
student1 = Student()
student1.introduce()
student1.study()
Output:
I am a person I am studying
Explanation:
- The Person class contains the introduce() method.
- The Student class inherits from Person.
- The Student class contains its own study() method.
- The student1 object is created from the Student class.
- The object can access introduce() because it is inherited from Person.
- It can also access study() because it belongs to the Student class.
Syntax of Inheritance:
class ParentClass:
# parent class members
class ChildClass(ParentClass):
# child class members
Explanation:
- ParentClass: The class whose attributes and methods can be inherited.
- ChildClass: The class that inherits from the parent class.
- ParentClass inside parentheses: Specifies the inheritance relationship.
- Child class members: Additional attributes and methods defined by the child class.
Example:
# Python program to demonstrate inheritance syntax
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
dog1 = Dog()
dog1.eat()
dog1.bark()
Output:
Eating
Barking
Here, Dog inherits from Animal, so the Dog object can use the eat() method defined in the Animal class.
Single Inheritance
Single inheritance occurs when one child class inherits from one parent class. It is the simplest type of inheritance in Python.
Example:
# Python program to demonstrate single inheritance
class Vehicle:
def start(self):
print("Vehicle started")
class Car(Vehicle):
def drive(self):
print("Car is driving")
car1 = Car()
car1.start()
car1.drive()
Output:
Vehicle started
Car is driving
Explanation:
- The Vehicle class contains the start() method.
- The Car class inherits from Vehicle.
- The Car class adds its own drive() method.
- The car1 object is created.
- The inherited start() method is called.
- The drive() method defined in Car is also called.
The inheritance structure is:

Multiple Inheritance
Multiple inheritance occurs when a child class inherits from more than one parent class. Python supports multiple inheritance, allowing a class to inherit functionality from multiple base classes.
Example:
# Python program to demonstrate multiple inheritance
class Father:
def father_property(self):
print("Father's property")
class Mother:
def mother_property(self):
print("Mother's property")
class Child(Father, Mother):
def child_property(self):
print("Child's property")
child1 = Child()
child1.father_property()
child1.mother_property()
child1.child_property()
Output:
Father's property
Mother's property
Child's property
Explanation:
- The Father class contains the father_property() method.
- The Mother class contains the mother_property() method.
- The Child class inherits from both Father and Mother.
- The Child object can access methods from both parent classes.
- It can also use its own methods.
The inheritance structure is:

Multilevel Inheritance
Multilevel inheritance occurs when a class inherits from another child class, creating multiple levels of inheritance. For example, Parent can inherit from Grandparent, and Child can then inherit from Parent.
Example:
# Python program to demonstrate multilevel inheritance
class Grandparent:
def house(self):
print("Grandparent has a house")
class Parent(Grandparent):
def car(self):
print("Parent has a car")
class Child(Parent):
def bike(self):
print("Child has a bike")
child1 = Child()
child1.house()
child1.car()
child1.bike()
Output:
Grandparent has a house
Parent has a car
Child has a bike
Explanation:
- Grandparent is the top-level parent class.
- Parent inherits from Grandparent.
- Child inherits from Parent.
- The Child class indirectly receives the functionality of Grandparent.
- The child1 object can access methods from all three levels.
The inheritance structure is:

Hierarchical Inheritance
Hierarchical inheritance occurs when multiple child classes inherit from the same parent class.
Example:
# Python program to demonstrate hierarchical inheritance
class Animal:
def eat(self):
print("Animal is eating")
class Dog(Animal):
def bark(self):
print("Dog is barking")
class Cat(Animal):
def meow(self):
print("Cat is meowing")
dog1 = Dog()
cat1 = Cat()
dog1.eat()
dog1.bark()
cat1.eat()
cat1.meow()
Output:
Animal is eating
Dog is barking
Animal is eating
Cat is meowing
Explanation:
- The Animal class contains the common eat() method.
- Dog inherits from Animal.
- Cat also inherits from Animal.
- Both child classes can use the inherited eat() method.
- Each child class also has its own specific method.
The inheritance structure is:

Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance. It may involve multiple, multilevel, or hierarchical inheritance together.
Example:
# Python program to demonstrate hybrid inheritance
class Animal:
def eat(self):
print("Animal is eating")
class Mammal(Animal):
def walk(self):
print("Mammal is walking")
class Bird(Animal):
def fly(self):
print("Bird is flying")
class Bat(Mammal, Bird):
def move(self):
print("Bat is moving")
bat1 = Bat()
bat1.eat()
bat1.walk()
bat1.fly()
bat1.move()
Output:
Animal is eating
Mammal is walking
Bird is flying
Bat is moving
Explanation:
- Animal is the base class.
- Mammal and Bird inherit from Animal.
- Bat inherits from both Mammal and Bird.
- The Bat object can access methods from the inheritance hierarchy.
- This creates a combination of hierarchical and multiple inheritance.
Using the super() Function
The super() function is used to access methods from a parent class or another class according to Python's method resolution order.
It is especially useful when a child class overrides a parent method or defines its own constructor but still needs to use the parent's implementation.
Example:
# Python program to demonstrate the super() function
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = course
student1 = Student("Tanavi", "BSc IT")
print(student1.name)
print(student1.course)
Output:
Tanavi
BSc IT
Explanation:
- The Person class contains an __init__() method that initializes name.
- The Student class inherits from Person.
- The Student constructor receives name and course.
- super().__init__(name) calls the parent class initialization method.
- The name attribute is initialized by the parent class.
- The course attribute is initialized by the child class.
Using super() helps avoid repeating the initialization code of the parent class.
Method Overriding in Inheritance
Method overriding occurs when a child class provides its own implementation of a method that already exists in the parent class.
Example:
# Python program to demonstrate method overriding
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
animal1 = Animal()
dog1 = Dog()
animal1.sound()
dog1.sound()
Output:
Animal makes a sound
Dog barks
Explanation:
- The Animal class defines a sound() method.
- The Dog class inherits from Animal.
- The Dog class defines its own sound() method.
- The child class implementation overrides the parent class implementation.
- When dog1.sound() is called, the Dog version is executed.
Method overriding is an important concept that allows different classes to provide their own behavior for the same method.
Inheritance with Constructors
Constructors can also be used with inheritance.
When a child class defines its own __init__() method, it can use super() to call the parent class's __init__() method when parent attributes need to be initialized.
Example:
# Python program to demonstrate inheritance with constructors
class Employee:
def __init__(self, name):
self.name = name
class Developer(Employee):
def __init__(self, name, language):
super().__init__(name)
self.language = language
developer1 = Developer("Tanavi", "Python")
print("Name:", developer1.name)
print("Language:", developer1.language)
Output:
Name: Tanavi
Language: Python
Explanation:
- The Employee class contains the name attribute.
- Developer inherits from Employee.
- Developer has its own __init__() method.
- super().__init__(name) calls the parent class's __init__() method.
- The child constructor initializes the additional language attribute.
- The object contains attributes initialized by both the parent and child classes.
Checking Inheritance Using isinstance() and issubclass()
Python provides the isinstance() and issubclass() functions to check relationships between objects and classes.
Using isinstance():
The isinstance() function checks whether an object is an instance of a particular class or one of its subclasses.
Example:
# Python program to demonstrate isinstance()
class Animal:
pass
class Dog(Animal):
pass
dog1 = Dog()
print(isinstance(dog1, Dog))
print(isinstance(dog1, Animal))
Output:
True
True
Explanation:
- Dog inherits from Animal.
- dog1 is an object of the Dog class.
- Therefore, dog1 is an instance of Dog.
- Since Dog inherits from Animal, dog1 is also considered an instance of Animal.
Using issubclass():
The issubclass() function checks whether one class is a subclass of another class.
Example:
# Python program to demonstrate issubclass()
class Animal:
pass
class Dog(Animal):
pass
print(issubclass(Dog, Animal))
print(issubclass(Animal, Dog))
Output:
True
False
Explanation:
- Dog inherits from Animal.
- Therefore, Dog is a subclass of Animal.
- Animal is not a subclass of Dog.
- issubclass() returns True when the specified inheritance relationship exists.
Method Resolution Order (MRO)
Method Resolution Order, commonly called MRO, defines the order in which Python searches for a method or attribute in an inheritance hierarchy.
MRO becomes especially important when using multiple inheritance because a class can have more than one parent class. Python uses the C3 linearization algorithm to determine the MRO. The mro() method or __mro__ attribute can be used to view the order.
Example:
# Python program to demonstrate Method Resolution Order
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
print(D.mro())
Output:
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
Explanation:
Python searches for a method in the following order:
D → B → C → A → object
If D does not contain the requested method, Python checks B. If it is not found there, it checks C, followed by A, and finally the base object class.
Understanding MRO is particularly useful when working with multiple inheritance and super().
Advantages of Inheritance
Inheritance provides several benefits when designing Object-Oriented Python programs.
- Code Reusability: Common methods and attributes can be written once in a parent class and reused by child classes.
- Less Code Duplication: Child classes do not need to repeatedly implement functionality already provided by the parent.
- Easy Maintenance: Common behavior can be managed in the parent class.
- Extensibility: Child classes can extend an existing class by adding new methods and attributes.
- Supports Polymorphism: Different child classes can provide different implementations of the same method.
- Better Organization: Inheritance allows related classes to be grouped into logical hierarchies.
- Real-World Modeling: Inheritance can represent relationships between general and specialized classes.
Common Beginner Mistakes
1. Forgetting to Specify the Parent Class: A child class must specify its parent class when inheritance is required.
Wrong:
class Animal:
def eat(self):
print("Eating")
class Dog:
def bark(self):
print("Barking")
Here, Dog does not inherit from Animal.
Correct:
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
Now Dog can use the eat() method inherited from Animal.
2. Forgetting self: Instance methods need self as their first parameter.
Wrong:
class Animal:
def eat():
print("Eating")
Correct:
class Animal:
def eat(self):
print("Eating")
3. Forgetting super() When Parent Initialization Is Required: When a child class defines its own __init__() method, it may need to call the parent constructor to initialize parent attributes.
Wrong:
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
self.course = course
In this example, the parent __init__() method is not called, so the name attribute is not initialized by the parent class.
Correct:
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = course
4. Using an Incorrect Parent Class Name: The class name used for inheritance must be defined correctly.
Wrong:
class Student(Persons):
pass
If Persons is not defined, Python will raise a NameError.
Correct:
class Person:
pass
class Student(Person):
pass
5. Confusing Inheritance with Object Creation: Defining a child class does not automatically create an object.
Example:
class Animal:
pass
class Dog(Animal):
pass
dog1 = Dog()
The dog1 object must be created explicitly when an object is required.
Conclusion
Inheritance is a powerful feature of Object-Oriented Programming in Python that allows one class to reuse and extend the functionality of another class. A child class can inherit accessible attributes and methods from a parent class and can also add new functionality or override existing methods. Python supports different types of inheritance, including single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance. The super() function is useful when a child class needs to access behavior from a parent class, while Method Resolution Order helps determine how Python searches for methods and attributes in an inheritance hierarchy. By understanding inheritance, developers can write reusable, organized, and maintainable Python programs. It also provides an important foundation for learning other OOP concepts such as polymorphism, encapsulation, and abstraction.
Frequently Asked Questions (FAQs)
1. What is inheritance in Python?
Inheritance is an Object-Oriented Programming feature that allows one class to acquire accessible attributes and methods from another class.
2. What is a parent class in Python?
A parent class is a class whose attributes and methods can be inherited by another class. It is also called a base class or superclass.
3. What is a child class in Python?
A child class is a class that inherits attributes and methods from another class. It is also called a derived class or subclass.
4. What are the types of inheritance in Python?
The commonly discussed types of inheritance in Python are single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance.
5. Does Python support multiple inheritance?
Yes. Python supports multiple inheritance, which allows a class to inherit from more than one parent class.
6. What is the use of the super() function in Python?
The super() function is used to access methods from a parent class or another class according to Python's Method Resolution Order. It is commonly used when a child class overrides a method or defines its own constructor.
7. What is method overriding in Python?
Method overriding occurs when a child class provides its own implementation of a method that is already defined in its parent class.
0 Comments