inheritance
In object-oriented programming (OOP), inheritance allows you to create a new class by inheriting features from an existing one. You can define a class that inherits data (attributes) and behavior (methods) from another class, known as the base class, superclass, or parent class.
This mechanism promotes code reuse and helps you create a hierarchical class structure that reflects real-world relationships. By using inheritance, you can override or extend the functionality of the parent class to tailor the behavior of the derived or child class to meet specific requirements.
Example
Here’s an example of inheritance in Python:
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def start_engine(self):
print(f"The {self.brand} {self.model}'s engine is starting!")
class SelfDrivingCar(Vehicle):
def self_drive(self):
print("Self-driving through the road!")
# Usage
car = SelfDrivingCar("Waymo", "One")
car.start_engine() # Output: The Waymo One's engine is starting!
car.self_drive() # Output: Self-driving through the road!
In this example, the SelfDrivingCar
class inherits from the Vehicle
class. Apart from .brand
, .model
and .start_engine()
, which are inherited members, the class implements the .self_drive()
method to provide extended functionality.
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:
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Inheritance and Composition: A Python OOP Guide (Course)
- Inheritance and Composition: A Python OOP Guide (Quiz)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (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)
By Leodanis Pozo Ramos • Updated April 24, 2025