Beginner Level
Intermediate Level
Advanced Level
Introduction
In this tutorial, we'll explore the concept of Single inheritance in Python. Single inheritance allows us to define a new class that inherits properties and methods from a parent class. We'll cover how to define a subclass. By the end of this tutorial, you'll have a solid understanding of how to use Single inheritance in Python to create efficient and well-structured code.
Table of Contents :
- What is single inheritance
- Implementing single inheritance in python
- super() function
- Code sample for single inheritance
- Overriding methods
- Accessing super-class methods
What is single inheritance :
- Single inheritance is a type of inheritance where a sub-class inherits properties and methods from a single super-class.
- super-class is also known as the parent class and sub-class is also known as child class.
- It is the simplest form of inheritance and is used when there is only one parent class from which the child class needs to inherit.
Implementing single inheritance in python
- To implement single inheritance in python, we specify the name of the parent class within parentheses while defining the child class.
- The syntax of implementing single inheritance in python is as follows :
class SubClass(SuperClass):
# class-body
super() function
- We can use the super() function in python to access members of the parent class.
- The
super()
function returns a temporary object of the super-class which allows the sub-class to call its methods. - We can initialize the data members of child class in the constructor as we normally do.
- The data members of the parent class can be initialized from the child class by calling the
__init__()
function of the parent class throughsuper()
function. - The syntax of calling parent class constructor from the child class is shown below. Here
arg_1, arg_2
are the values used to initialize the data members of the child class whilearg_3, arg_4
are the values passed to parent-class to initialize its data members.
class SubClass(SuperClass):
def __init__(self, arg_1, arg_2, arg_3, arg_4):
self.var_1 = arg_1
self.var_2 = arg_2
super().__init__(arg_3, arg_4)
Code sample for single inheritance
Below we have a full working example of single inheritance in python
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
class Car(Vehicle):
def __init__(self, make, model, utility, transmission):
self.utility = utility
self.transmission = transmission
super().__init__(make, model)
def show_details(self):
print(f"The make of the Car is : {self.make}")
print(f"The model of the Car is : {self.model}")
print(f"The utility of the Car is : {self.utility}")
print(f"The transmission of the Car is : {self.transmission}")
my_car = Car("Audi", 2023, "SUV", "Automatic")
my_car.show_details()
# Output
# The make of the Car is : Audi
# The model of the Car is : 2023
# The utility of the Car is : SUV
# The transmission of the Car is : Automatic
Overriding methods :
- Sub-class can override the methods of their super-class by defining a method with the same name.
- The sub-class method will be called instead of the super-class method when a method is called on an instance of the sub-class.
- Code Sample :
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
def show_details(self):
print(f"The make of the Vehicle is : {self.make}")
print(f"The model of the Vehicle is : {self.model}")
print()
class Car(Vehicle):
def __init__(self, make, model, utility, transmission):
self.utility = utility
self.transmission = transmission
super().__init__(make, model)
# Overriding method of the super class
def show_details(self):
print(f"The make of the Car is : {self.make}")
print(f"The model of the Car is : {self.model}")
print(f"The utility of the Car is : {self.utility}")
print(f"The transmission of the Car is : {self.transmission}")
print()
my_vehicle = Vehicle("BMW", 2022)
my_car = Car("Audi", 2023, "SUV", "Automatic")
# Calling show_details method of super class
my_vehicle.show_details()
# Calling show_details method of sub class
my_car.show_details()
# Output
# The make of the Vehicle is : BMW
# The model of the Vehicle is : 2022
# The make of the Car is : Audi
# The model of the Car is : 2023
# The utility of the Car is : SUV
# The transmission of the Car is : Automatic
Accessing super-class methods :
- Sub-class can access the methods of their Super-class by using the
super()
function followed by the method name. - Code Sample :
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
def show_details(self):
print(f"The make of the Vehicle is : {self.make}")
print(f"The model of the Vehicle is : {self.model}")
print()
class Car(Vehicle):
def __init__(self, make, model, utility, transmission):
self.utility = utility
self.transmission = transmission
super().__init__(make, model)
# Overriding method of the super class
def show_details(self):
# Calling show_details method of super class
super().show_details()
print(f"The utility of the Car is : {self.utility}")
print(f"The transmission of the Car is : {self.transmission}")
print()
my_car = Car("Audi", 2023, "SUV", "Automatic")
my_car.show_details()
# Output:
# The make of the Vehicle is : Audi
# The model of the Vehicle is : 2023
# The utility of the Car is : SUV
# The transmission of the Car is : Automatic
Prev. Tutorial : Inheritance
Next Tutorial : Multiple inheritance in Python