Introduction to Inheritance
Inheritance is an OOP concept that allows one class to inherit attributes and methods from another class. The class that is being inherited from is called the “base” or “parent” class, while the class that is inheriting is called the “derived” or “child” class.
Inheritance in Python
To inherit from a parent class in Python, simply include the parent class name in parentheses after the child class name.
Example:
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
print("Woof!")
class Cat(Animal):
def speak(self):
print("Meow!")
Overriding Methods
Subclasses can override methods of the superclass by defining a method with the same name. The new method will be used instead of the superclass method.
Example:
class Animal:
def make_sound(self):
print("The animal makes a sound")
class Dog(Animal):
def make_sound(self):
print("Woof!")
my_dog = Dog()
my_dog.make_sound() # Output: Woof!
Polymorphism
Polymorphism allows different classes to be treated as instances of the same class, based on their shared properties and methods. This can simplify code by allowing you to use a single interface for different objects.
Example:
class Animal:
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
print("Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
animals = [Dog(), Cat()]
for animal in animals:
animal.make_sound()
Hands-on Exercises
- Create a Python script that defines a
'Vehicle'class with attributes for speed and color, and methods for accelerating and braking. Create'Car'and'Motorcycle'subclasses that inherit from.'Vehicle' - Create a Python script that defines a
'Shape'class with a method for calculating the area. Create'Circle','Square', and'Triangle'subclasses that inherit from'Shape'and override the area calculation method.
Real-World Example
Inheritance and polymorphism can be used to model a variety of real-world scenarios. For example, you could create a class hierarchy for different types of employees in a company, such as managers, engineers, and salespeople, with shared methods for calculating salaries and bonuses.
Additional Resources
- Python documentation: https://docs.python.org/3/tutorial/classes.html#inheritance
- Real Python: https://realpython.com/inheritance-composition-python/
- W3Schools Python tutorial: https://www.w3schools.com/python/python_inheritance.asp
Review Questions
- What is inheritance in Python, and how can it be used to create subclasses?
- Explain how to override methods in a subclass.
- What is polymorphism, and how can it be used to simplify code?
- Create a Python script that defines a
'Person'class and subclasses for'Student'and'Teacher', with appropriate attributes and methods.
Summary and Next Steps
In this lesson, you have learned about:
- Inheritance in Python
- Overriding methods in child classes
- Polymorphism in Python
In the next lesson, you will learn about magic methods and operator overloading in Python, which can make your classes more expressive and easier to use. Remember to review the material periodically and practice your skills to reinforce your learning. Good luck on your journey to becoming a Python developer!



