DEV Community

Cover image for Intro to Python: Day 9 - Class Inheritance (OOP)
James Hubert
James Hubert

Posted on

Intro to Python: Day 9 - Class Inheritance (OOP)

Hi there πŸ‘‹ I'm a New York City based web developer documenting my journey with React, React Native, and Python for all to see. Please follow my dev.to profile or my twitter for updates and feel free to reach out if you have questions. Thanks for your support!

Today we will follow up on our discussions of encapsulation and abstraction by discussing the third major pillar of Object Oriented Programming, inheritance.

According to Wikipedia, "inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation". In other words, when an object-oriented programming language allows for inheritance, we can create objects which inherit methods and data from a parent class. This makes it easy to write new classes which inherit from a parent but also have their own functionality or data which does not exist on the parent.

Let's take the example of a Vehicle class. Maybe there are some shared attributes and abilities that, no matter what kind of vehicle you have, it will be true. For example, all vehicles require energy. For simplification, we'll say they all take gas, and they all have a brand name.

Here is an example of a Toyota Camry instance of a Vehicle which uses a constructor to set miles per gallon, brand name, and model. It also has a getter method that prints the brand and model of the vehicle.

class Vehicle:
    def __init__(self, mpg, brand, model):
        self.mpg = mpg
        self.brand = brand
        self.model = model

    def get_vehicle_name(self):
        print(f"{self.brand} {self.model}")
Enter fullscreen mode Exit fullscreen mode

We can now create child classes that inherit all of the methods and data from the Vehicle class, but also have their own data and methods unique to them. Let's make a Scooter class, which is a subset of vehicles, and have it inherit the Vehicle class.

class Scooter(Vehicle):
    def __init__(self, mpg, color):
        super().__init__(mpg, "Vespa", "PX")
        self.__color = color

    def get_color(self):
        print("f{self.__color}")
Enter fullscreen mode Exit fullscreen mode

In the Scooter class, we pass the Vehicle class in so that Scooter inherits the parent's data and methods. In the constructor we can then use the super() call to call the parent's constructor. In the case of our scooter class, I always want the scooter to be a Vespa brand PX model scooter, so I pass those in and restrict the user from being able to make their own scooter. I then set a private variable __color in the constructor because we all know Vespa owners care a lot about their flashy colors.

Like on the vehicle component, mpg, brand and model are all set in the constructor method, but we also have a unique method for the Scooter child called get_color() which we can use to print the color of the vehicle.

Practice Can you practice class inheritance by making a new class called Truck that inherits the Vehicle class but also takes a has_trailer_hitch boolean value in the constructor, and has a getter method to print out either Has trailer hitch or Does not have trailer hitch?

If you like projects like this and want to stay up to date with more, check out my Twitter @stonestwebdev, I follow back! See you tomorrow for another project.

Top comments (0)