-
Hajipur, Bihar, 844101
Inheritance is one of the core ideas in Object Oriented Programming. It allows one class to take features from another class. This helps you avoid repeating the same code again and again. With inheritance, you can build classes that share common behaviour while still having their own unique features.
In this tutorial, you will learn how inheritance works in Python, how to create parent and child classes, how to override methods, and how to use different types of inheritance in real programs.
Inheritance means creating a new class from an existing one. The original class is called the parent class or base class, and the new class is called the child class or derived class.
The child class can:
Use everything from the parent class
Add new features
Change existing behaviour if needed
This makes your code cleaner and easier to manage.
A parent class contains common attributes and methods that other classes can reuse.
Example:
class Animal:
def __init__(self, name):
self.name = name
def sound(self):
print("This animal makes a sound.")
This class holds a name and a simple method.
A child class inherits everything from the parent class. To do this, you write the parent class name inside brackets.
Example:
class Dog(Animal):
pass
Now Dog is a child class of Animal.
You can create an object of the child class and still access methods from the parent class.
Example:
d1 = Dog("Bruno")
d1.sound()
Even though Dog does not define sound(), it inherits it from Animal.
You can add extra methods inside the child class.
Example:
class Dog(Animal):
def bark(self):
print("Dog barks loudly.")
Usage:
d1 = Dog("Bruno")
d1.bark()
The parent class methods and child class methods can both be used.
Sometimes you want the child class to have a different version of a method. This is called method overriding.
Example:
class Dog(Animal):
def sound(self):
print("Dog barks.")
Now if you call:
d1.sound()
It will use the child’s version instead of the parent’s version.
super() in Child ClassesThe super() function lets you access methods from the parent class.
Example:
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
Here, super() runs the parent class __init__() method.
Let’s create a base class Vehicle.
class Vehicle:
def __init__(self, brand, price):
self.brand = brand
self.price = price
def info(self):
print("Brand:", self.brand)
print("Price:", self.price)
Now a child class Car:
class Car(Vehicle):
def __init__(self, brand, price, seats):
super().__init__(brand, price)
self.seats = seats
def details(self):
self.info()
print("Seats:", self.seats)
Creating an object:
c1 = Car("Toyota", 1200000, 5)
c1.details()
The Car class uses the parent’s info() method and adds its own feature.
Python supports different types of inheritance. Here are the most common ones.
One child from one parent.
Example:
class A:
pass
class B(A):
pass
One child class inherits from more than one parent.
Example:
class A:
pass
class B:
pass
class C(A, B):
pass
This should be used carefully to avoid confusion.
A class inherits from another class, which itself inherits from another.
Example:
class A:
pass
class B(A):
pass
class C(B):
pass
One parent class has multiple child classes.
Example:
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
A mix of different inheritance types. Python allows this but you must design it clearly to avoid complexity.
Inheritance helps you:
Avoid writing the same code multiple times
Organise your classes better
Change common behaviour in one place
Build clean, structured programs
Create flexible systems that grow easily
Inheritance is used in almost every real Python application.
Parent and Child Class
class Person:
def intro(self):
print("I am a person.")
class Student(Person):
pass
Child Class with Extra Method
class Student(Person):
def study(self):
print("I am studying.")
Overriding Methods
class Student(Person):
def intro(self):
print("I am a student.")
Using super()
class Student(Person):
def __init__(self, name):
super().__init__()
self.name = name
Multilevel Example
class A:
pass
class B(A):
pass
class C(B):
pass
Multiple Inheritance
class A:
pass
class B:
pass
class C(A, B):
pass
Calling Parent Method After Overriding
class Student(Person):
def intro(self):
super().intro()
print("And I am also a student.")
Vehicle Example
class Bike(Vehicle):
def __init__(self, brand, price):
super().__init__(brand, price)
Adding New Attributes
class Car(Vehicle):
def __init__(self, brand, price, type):
super().__init__(brand, price)
self.type = type
Using Inheritance for Calculations
class Shape:
def area(self):
return 0
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
Inheritance lets you build new classes using the features of existing ones. The parent class holds the basic structure, while the child class can reuse, extend, or change that behaviour. Python supports single, multiple, multilevel, hierarchical, and hybrid inheritance. Using inheritance helps keep your code organised, clean, and flexible, especially in large applications.
Q1. Write a Python program to create a parent class Employee with properties name and salary.
Q2. Write a Python program to create a child class Manager that inherits from Employee.
Q3. Write a Python program to add a method in the Employee class to display employee details.
Q4. Write a Python program to add an extra method in the Manager class to display department or role-specific info.
Q5. Write a Python program to use super() in the Manager class constructor to call the parent constructor.
Q6. Write a Python program to override a method from the Employee class inside the Manager class.
Q7. Write a Python program to create a parent class Shape and a child class Circle that inherits from it.
Q8. Write a Python program to add __init__() to both Shape and Circle and use inheritance to pass values.
Q9. Write a Python program to demonstrate multiple inheritance using two classes Father and Mother.
Q10. Write a Python program to call both parent methods from a child class object, using method resolution order (MRO).