Single Inheritance in Python (with Example)
Single inheritance, as the name suggests, allows a class to inherit properties and behavior from a single parent class. In other words, when a child class inherits only a single parent class, it is called a single inheritance in Python.
In a single inheritance, we inherit properties (i.e. variables) and behavior (i.e. methods) from the parent class or base class and use them into the child class or derived class.
We can access all the data members (i.e. properties) and methods defined in the parent class from the child class. In the child class, we can also add specific methods as well as may also override or provide its implementation for the parent class’s methods.
In the below figure, class B inherits from class A. So, class B is a derived class or child class and class A is a parent class or base class. This relationship represents a single inheritance.
Syntax to Define Single Inheritance in Python
In Python, a child class can inherit the parent class by simply mentioning the parent class after the child class name in parentheses. To inherit the parent class into the child class, use the following syntax as shown below:
class ParentClass:
# ParentClass attributes and methods
class ChildClass(ParentClass):
# ChildClass attributes and methods
The above syntax is very simple and straightforward than that is used in Java for inheriting classes. The ChildClass represents the name of derived class and the ParentClass in the parentheses represents the name of the existing class followed by a colon. Remember that the body or block of class begins with indentation.
Simple Single Inheritance Example Program
Let’s take some simple example programs based on the single inheritance in Python.
Example 1:
# Python program to demonstrate single inheritance.
# Creating a base class named Parent.
class Parent:
# Defining a function inside the base class.
def m1(self):
print("Parent class method")
# Creating a derived class named Child.
class Child(Parent):
# Defining a function inside the derived class.
def m2(self):
print("Child class method")
# Outside the class definition.
# Creating an instance of child.
child = Child()
# Calling functions of both base and derived classes.
child.m1()
child.m2()
# Creating an instance of Parent class.
parent = Parent()
# Calling the function of base class.
parent.m1()
Output:
Parent class method
Child class method
Parent class method
In the above example code, the Child class inherits the m1() method from its Parent class. The Child class has also has its own m2() method defined. Note that “parent” is an instance of Parent class and can only access m1() method because the reference variable parent refers to the object of Parent class.
Whereas, “child” is an instance of Child class and can access both m1() and m2() methods from Parent class and its own class because the reference variable child is pointing to the object of Child class.
With inheritance, the method from the Parent class automatically comes in the Child class by default. In other words, by default, the method from the Parent class is copied into the Child class. Therefore, we can access both methods from the Parent class as well as Child class using object reference variable child.
Example 2:
# Python program to demonstrate accessing parent class method in the child class.
# Creating a base class named Animal.
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def bark(self):
# Accessing parent class method inside the child class method.
return self.speak() + " and barks"
# Creating an instance of the Dog class.
dog = Dog()
# Calling a method from the child class that accesses the parent class method
print(dog.bark())
Output:
Animal speaks and barks
In this example, the Animal class defines a method called speak(), which returns the string “Animal speaks”. The Dog class inherits from the Animal class using the statement Dog(Animal), meaning that the Dog class has access to the speak() method defined in the Animal class.
In the bark() method of the Dog class, we have called self.speak() to access and reuse the speak() method from the parent class. By doing so, the Dog class not only inherits the speak() method but also extends it to include the additional new feature.
This demonstrates reusability because the Dog class doesn’t need to redefine the speak() method; it reuses the functionality defined in the Animal class.
If you create another class, say Cat, that also inherits from Animal, it could similarly reuse the speak() method without duplicating its code. Thus, we can achieve code reusability, which is a fundamental principle of object-oriented programming.
Example 3:
# Define a class called Teacher.
class Teacher:
# Constructor method with a parameter 'name'
def __init__(self, name):
self.name = name
# Declare a method in the parent class.
def showMe(self):
print("Teacher name: ", self.name)
# Define a class named Student that inherits from Teacher.
class Student(Teacher):
# Overriding the showMe() method of the parent class.
def showMe(self):
print("Student name: ", self.name)
# Outside the class definition.
# Creating an instance of Teacher class.
teach = Teacher("Deepak")
teach.showMe() # calling method of the Teacher class.
# Creating an instance of Student class
st = Student("Mahika")
st.showMe() # calling method of Student class.
Output:
Teacher name: Deepak
Student name: Mahika
In this part of the code, we have defined a class called Teacher. This class has two methods: __init__() and showMe(). The __init__() method (constructor) takes one parameter, name, and initializes an instance variable self.name with the value of name. This method is called when an object of the class is created.
Then, we have defined a new class called Student, which inherits from the Teacher class. This means that the Student class will inherit the attributes and methods of the Teacher class.
Inside the Student class, we have defined a showMe() method that overrides the showMe() method of the parent class (Teacher). This is known as method overriding, where a child class provides its own implementation of a method that is already defined in the parent class.
Outside the class definition, we have created an instance of the Teacher class by passing the argument value “Deepak” to the constructor parameter name. Then, we called the showMe() method of parent class using the object reference variable teach, which displays “Teacher name: Deepak” because the reference variable is pointing to the object of parent (Teacher) class.
After it, we have created an instance of the Student class by passing the argument “Mahika” to the constructor parameter. When we called the showMe() method on the st object, it displays “Student name: Mahika” because the Student class has overridden the showMe method to provide its own implementation.
Thus, this code demonstrates the concept of inheritance as well as method overriding in object-oriented programming system. The Student class inherits from the Teacher class and provides its own implementation inside showMe() method.
Example 4:
class Base:
def m1(self, x):
print("Parent class m1 method")
self.x = x
print("Value of x = ", self.x)
class Derived(Base):
def m2(self, y):
print("Child class m2 method")
self.y = y
print("Value of y = ", self.y)
# Creating an instance of class Derived.
obj = Derived()
# Calling methods of Derived class.
obj.m1(10)
obj.m2(20)
Advanced Single Inheritance Examples for Best Practice
Let’s take some advanced example programs based on the single inheritance in Python for the best practice.
Example 5:
# Superclass
class Shape:
# Constructor method of superclass.
def __init__(self):
self.length = float(input("Enter the length: "))
self.breadth = float(input("Enter the breadth: "))
# Display() method of superclass.
def display(self):
print("Length = ", self.length)
print("Breadth = ", self.breadth)
# Subclass.
class Rectangle(Shape):
# Constructor method of subclass.
def __init__(self):
# Calling the constructor method of the superclass.
Shape.__init__(self)
def cal_per(self):
self.per = 2 * (self.length + self.breadth)
print("Perimeter of rectangle = ", self.per)
def cal_area(self):
self.area = self.length * self.breadth
print("Area of rectangle = ", self.area)
# Create an object of class Rectangle.
rt = Rectangle()
# Calling method of subclass using rt.
rt.display()
rt.cal_per()
rt.cal_area()
Output:
Enter the length: 10.5
Enter the breadth: 20.3
Length = 10.5
Breadth = 20.3
Perimeter of rectangle = 61.6
Area of rectangle = 213.15
In this example, we have constructed a superclass Shape which contains two data members, namely length and breadth, as shown in the constructor method __init__() of superclass Shape. The superclass Shape contains an instance method display(), which prints the value of length and breadth.
After it, we have created a subclass Rectangle which is inheriting the features of its superclass Shape. By deriving, the members length, breadth, and display() method of superclass Shape will become the members of subclass Rectangle. This class also has its own members cal_per() and cal_area() which will calculate perimeter and area of rectangle and will display the result on the console.
Now, we have created an instance of Rectangle class and called the display(), cal_per(), and cal_area() methods, respectively. Note that in the above program, we have not constructed the instance of superclass Shape. We have just created an instance of derived class Rectangle and accessed the data members and methods of base class.
As we have created an instance of subclass Rectangle, the constructor method __init__() of subclass Rectangle is called which further calls the constructor method of superclass Shape.
The __init__() method of superclass takes the input as length and breadth from the users and stores them into instance variables self.length and self.breadth. Here, the parameter “self” represents the current object.
Subsequently, rt.display() calls the superclass method display(), which displays the values of length and breadth of the rectangle. At the end, rt.cal_per() calls the subclass method cal_per(), which computes the perimeter of the rectangle and displays appropriate the result.
Similarly, rt.cal_area() calls the subclass method cal_area(), which computes the area of the rectangle and displays appropriate the result on the console.
Example 6:
# Parent class
class Vehicle:
# Parent class constructor.
def __init__(self, brand):
self.brand = brand # instance variable.
# Parent class method.
def start_engine(self):
return f"{self.brand} engine started"
# Derived class.
class Car(Vehicle):
# Derived class constructor.
def __init__(self, brand, model):
# Calling constructor of parent class using super() function.
super().__init__(brand)
self.model = model # instance variable.
# Overriding parent class method with specific implementation inside derived class.
def start_engine(self):
return f"{self.brand} {self.model} car's engine started"
# Creating an instance of the child class
car = Car("Toyota", "Camry")
# Calling overridden methods.
print(car.start_engine())
Output:
Toyota Camry car's engine started
In this tutorial, we have explained single inheritance in Python with various example programs. Hope that you will have understood the basic definition and syntax of single inheritance and practiced all programs. In the next, we will learn multilevel inheritance in Python with the help of advanced example programs for the best practice.
Thanks for reading!!!



