Creating Subclass in Python
In this tutorial, we will learn how to create subclass in Python.
Let’s discuss classes first and then move on to subclasses in Python.
You should have basic knowledge of Concept of Inheritance, Superclass and Subclass in Python
A class is a container that has the properties and the behavior of an object.
It is a blueprint of an object.
Let’s build a sample class:
class Employee: # base class def __init__(self,name,age): self.name = name # these are properties of any object of this class self.age = age def display_name(self): # this is the behavioural part print (self.name) e1 = Employee("Jagannath",20) # e1 is an object of employee class e1.display_name() # it has all the properties and methods mentioned above
Output : Jagannath
Certainly, this is how a class is created, and the objects of that class are used for different purposes.
Now, the concept is to deal with the Subclass in Python.
A subclass in Python :
Before jumping right into the code, we shall discuss why a subclass should be used.
Following are the reasons to do so,
- inherits all the properties and methods of the base class or superclass.
- easy to modify the existing methods in a class.
- Reusability of code
Syntax to create a subclass in Python:
class <class_name>( base_class name) :
Let’s understand by an example,
class Employee: # base class def __init__(self,name,age): self.name = name # these are properties of any object of this class self.age = age def display_name(self): # this is the behavioural part print (self.name) class Manager(Employee): # creating Sub_class or derived class pass m1 = Manager("Pavan",13) m1.display_name() print (m1.age)
Output : Pavan 13
There should be no surprise how the code is executed, because of the following reasons,
- The Manager class is derived class or subclass of Employee.
- Therefore, it inherits all the properties and methods of the Employee class.
- This is the advantage of subclass in OOP.
Function to check whether a class is subclass or not in Python:
In Python, there is an inbuilt function issubclass() which checks whether the class is a derived class or not.
Syntax of issubclass() function :
issubclass(subclass_name , superclass_name) : It returns the following,
- Returns True if the class is a subclass of another class.
- If the class is not a subclass, then it returns False.
Moreover, the best way to understand is by an example,
Here it is :
class Employee: # base class def __init__(self,name,age): self.name = name # these are properties of any object of this class self.age = age def display_name(self): # this is the behavioural part print (self.name) class Manager(Employee): # creating Sub_class or derived class pass # pass statement is to keep that function or class definition empty result = issubclass(Manager,Employee) # Manager class is Sub_class of Employee print (result)
Output : True
Concluding, it should be very clear how to create a subclass and also check whether it’s a subclass or not as it forms the base of Inheritance in Python.
Very well explained!! Got an understanding of classes from this. Thanks!