Opens in a new tab
Lesson 2

Inheritance and Polymorphism in Python

Object-Oriented Programming, Python Mastery Program, Stage 2 Python

Written by

Muhammad Atif

Last Updated

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:

Python
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:

Python
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:

Python
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

  1. 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'.
  2. 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

Review Questions

  1. What is inheritance in Python, and how can it be used to create subclasses?
  2. Explain how to override methods in a subclass.
  3. What is polymorphism, and how can it be used to simplify code?
  4. 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!

Muhammad Atif

Data Analyst & Web Development Professional | Focused on Machine Learning, SQL and Python

Transitioned from a background in finance to IT, I now focus on data science and web development, with specialties in Machine Learning, SQL, and Python. My articles aim to clarify complex IT concepts, informed by a rich history in analytical thinking and strategy.

You Might Also Like

Browse more topics and insights handpicked just for you. Stay updated and informed with our latest articles and news.