subclass
In Python, a subclass is a class that inherits from another class, known as its base class, superclass, or parent class.
When you create a subclass, you define a new class based on an existing one, which allows you to reuse code and extend functionality. This is a key feature of object-oriented programming (OOP) called inheritance, which promotes code reuse and modularity.
By inheriting from a superclass, a subclass gains access to its attributes and methods. At the same time, the subclass can override these attributes and methods, extend them, or introduce new ones. This allows for customization and specialization of the subclass. Creating subclasses is a common way to model relationships and hierarchies in programming.
Example
Here’s an example demonstrating how you can create and use a subclass in Python:
aircraft.py
class Aircraft:
def __init__(self, lift, max_speed):
self.lift = lift
self.max_speed = max_speed
def fly(self):
print(f"{type(self).__name__} is flying...")
def show_technical_specs(self):
print(f"Lift: {self.lift} kg")
print(f"Max speed: {self.max_speed} km/h")
class Helicopter(Aircraft):
def __init__(self, lift, max_speed, num_rotors):
super().__init__(lift, max_speed)
self.num_rotors = num_rotors
def show_technical_specs(self):
super().show_technical_specs()
print(f"Number of rotors: {self.num_rotors}")
In this example, Helicopter
is a subclass of Aircraft
. The subclass extends the initializer method of Aircraft
by adding a new attribute, .num_rotors
. It also extends .show_technical_specs()
by calling super()
to reuse the superclass’s implementation while adding helicopter-specific details.
You can create an instance of the subclass to confirm that it inherits .fly()
from the superclass and that the extended methods works as intended:
>>> from aircraft import Helicopter
>>> bell_206 = Helicopter(lift=1450, max_speed=240, num_rotors=2)
>>> bell_206.show_technical_specs()
Lift: 1450 kg
Max speed: 240 km/h
Number of rotors: 2
>>> bell_206.fly()
Helicopter is flying...
By creating a subclass based on Aircraft
, you avoid duplicating generic functionality shared across different aircraft types. At the same time, you can extend or override functionality specific to helicopters.
Related Resources
Tutorial
Inheritance and Composition: A Python OOP Guide
In this step-by-step tutorial, you'll learn about inheritance and composition in Python. You'll improve your object-oriented programming (OOP) skills by understanding how to use inheritance and composition and how to leverage them in their design.
For additional information on related topics, take a look at the following resources:
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Inheritance and Composition: A Python OOP Guide (Course)
- Inheritance and Composition: A Python OOP Guide (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)