Home »
Python
Python Create a Subclass
Last Updated : May 03, 2025
Python allows you to create subclasses that inherit properties and methods from a parent (or base) class. In this chapter, we will learn how to create and use subclasses in Python with the help of examples.
Creating a Subclass
To create a subclass, define a new class and pass the name of the parent class in parentheses.
Example
In this example, we define a Person
class and a subclass Student
that inherits from it.
# Defining the parent class
class Person:
def __init__(self, name):
self.name = name
def introduce(self):
print(f"My name is {self.name}")
# Creating a subclass
class Student(Person):
def study(self):
print(f"{self.name} is studying")
# Creating an object of the subclass
student1 = Student("Kavya")
# Using methods from both parent and subclass
student1.introduce()
student1.study()
The output of the above code would be:
My name is Kavya
Kavya is studying
Using the super()
Function
The super()
function allows you to call the constructor or methods of the parent class from the subclass.
Example
In this example, we use super()
to initialize the parent class from the subclass constructor.
# Defining the parent class
class Person:
def __init__(self, name):
self.name = name
# Creating a subclass and calling parent constructor
class Student(Person):
def __init__(self, name, roll_no):
super().__init__(name)
self.roll_no = roll_no
def display(self):
print(f"Name: {self.name}, Roll No: {self.roll_no}")
# Creating object of subclass
student2 = Student("Laksh", 101)
student2.display()
The output of the above code would be:
Name: Laksh, Roll No: 101
Overriding Methods in a Subclass
A subclass can override the methods of the parent class to change or extend its behavior.
Example
In this example, the introduce()
method is overridden in the Student
subclass.
# Defining the parent class
class Person:
def introduce(self):
print("Hello, I am a person.")
# Subclass overriding a method
class Student(Person):
def introduce(self):
print("Hello, I am a student.")
# Creating objects
person1 = Person()
student3 = Student()
# Calling introduce method
person1.introduce()
student3.introduce()
The output of the above code would be:
Hello, I am a person.
Hello, I am a student.
Check Inheritance Using issubclass()
and isinstance()
Use issubclass()
to check if a class is a subclass of another class, and isinstance()
to check if an object is an instance of a specific class.
Example
In this example, we check subclass and instance relationships.
class Person:
pass
class Student(Person):
pass
# Checking subclass
print(issubclass(Student, Person)) # True
# Checking instance
s = Student()
print(isinstance(s, Student)) # True
print(isinstance(s, Person)) # True
The output of the above code would be:
True
True
True
Exercise
Select the correct option to complete each statement about creating subclasses in Python.
- To create a subclass in Python, use the syntax class SubClassName(___).
- The super() function is typically used to ___.
- If class
Dog
is a subclass of Animal
, then Dog
___ the properties and methods of Animal
.
Advertisement
Advertisement