Create a Python Subclass
Last Updated :
16 Feb, 2024
Python, renowned for its simplicity and versatility, empowers developers to write clean and maintainable code. One of its powerful features is inheritance, which allows the creation of subclasses that inherit properties and behaviors from parent classes. Subclassing enables code reuse, promotes modularity, and facilitates extensibility. In this article, we’ll delve into the fundamentals of creating Python subclasses, exploring various aspects with multiple examples.
What is Python SubClass?
In Python, a subclass is a class that inherits attributes and methods from another class, known as the superclass or parent class. When you create a subclass, it can reuse and extend the functionality of the superclass. This allows you to create specialized versions of existing classes without having to rewrite common functionality. To create a subclass in Python, you define a new class and specify the superclass in parentheses after the class name.
Syntax :
class SubclassName(BaseClassName):
# Class attributes and methods for the subclass
# …
How To Create A Python Subclass?
Below is the step-by-step guide to How To Create A Python Subclass.
Example 1: Creating a simple subclass
Animal
is the base class with a __init__
method to initialize the name
attribute and a make_sound
method (abstract in this case, as it doesn’t have a specific implementation).
Dog
is a subclass Animal
that inherits from it. It overrides the make_sound
method to provide a specific implementation for dogs.
- Instances of both classes are created, and we demonstrate how attributes and methods can be accessed.
Python3
class Animal:
def __init__( self , name):
self .name = name
def make_sound( self ):
pass
class Dog(Animal):
def make_sound( self ):
return "Woof!"
generic_animal = Animal( "Generic Animal" )
dog_instance = Dog( "Buddy" )
print (generic_animal.name)
print (dog_instance.name)
print (dog_instance.make_sound())
|
Example 2: Adding additional attributes in the subclass
Shape
is the base class with an __init__
method to initialize the color
attribute and an abstract area
method.
Circle
is a subclass of Shape
that extends it by adding a radius
attribute. It calls the superclass constructor using super()
to initialize the common attribute.
- The
area
method is overridden in the Circle
subclass to provide a specific implementation for calculating the area of a circle.
- Instances of both classes are created, and we demonstrate how attributes and methods can be accessed.
Python3
class Shape:
def __init__( self , color):
self .color = color
def area( self ):
pass
class Circle(Shape):
def __init__( self , color, radius):
super ().__init__(color)
self .radius = radius
def area( self ):
return 3.14 * self .radius * * 2
generic_shape = Shape( "Red" )
circle_instance = Circle( "Blue" , 5 )
print (generic_shape.color)
print (circle_instance.color)
print (circle_instance.radius)
print (circle_instance.area())
|
Conclusion
In conclusion, creating a Python subclass involves defining a new class that inherits attributes and methods from an existing class, known as the base class or superclass. Subclasses can extend or override the functionality of the base class, allowing for code reuse and customization. Through the use of the super()
function, subclasses can invoke the constructor of the superclass to initialize shared attributes.
Share your thoughts in the comments
Please Login to comment...