Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Inheritance and Polymorphism

00:00 Welcome to your next lesson in Object-Oriented Programming in Python versus Java. In your previous lessons, we’ve looked at how Python implements encapsulation.

00:10 We’ve been able to define all of the attributes and methods that our objects are going to use and we’ve provided access to them. In these next few lessons, we’re going to take a look at how Python implements inheritance and polymorphism.

00:25 Remember, inheritance allows one class to derive all of the methods and attributes from a superclass. Typically, we have a general object description as our superclass, like a vehicle, and then our subclasses are more specific versions—a car or a boat.

00:44 Those things that we view common to any vehicle, we define in the Vehicle class. We then say Car and Boat would inherit from that Vehicle class.

00:56 And then we would put into those subclasses details specific to a car or a boat. Polymorphism allows you to use the same name for different subclass actions—attributes, methods—and the correct one will be used based on what type of object calls it.

01:20 So, for example, if we tell a Vehicle how to paint itself and we then define a Car and a Boat, then we can simply paint those using the same task that we gave to the superclass.

01:37 Let’s take a look first at inheritance.

01:41 So here, we’re going to define a class Vehicle, and in this model, we’re going to say .color and .model are attributes of any vehicle.

01:53 And just to give us something different to compare to, we’ll say .year is a feature we’re only interested in referring to cars. So we define the class Vehicle and write the code necessary to handle .color and .model. And then to say class Car inherits from Vehicle, we put the superclass Vehicle in parentheses, and that’s how we say that Car inherits or extends Vehicle.

02:22 A more detailed version here. We can see that my initializer for Vehicle assigns color and model to the appropriately newly-created attributes, and any methods that we have written to deal with color go in the Vehicle class as well—the .setColor(), the painting of the vehicle. In class Car, we have said that it inherits from Vehicle.

02:46 We don’t need to redefine the attributes .color and .modelwe already have them. We don’t need to redefine a .setColor() method.

02:56 The superclass one already does what we want it to do. We just have to provide functionality for those things that are new for cars. For example, the class variable for the number of wheels, the year of manufacturer, the number of cup holders, voltage—all of those things that we are putting specific to a car are defined in the Car class. And any methods or actions—in this case, the property setter and getters for ._voltage—go in the Car class, since that’s where that particular attribute is defined.

03:31 Just like in Java, you have to let the superclass deal with the parameters to the initializer that it’s responsible for. Instead of using the keyword super, however, we specify the superclass, whose method we want to use.

03:48 So here, we’re going to say “Let’s use Vehicle’s .__init__() method for color and model.” This would be equivalent to saying super(color, model) in Java.

04:00 The syntax in Python to use a superclass field method is to precede the method call with the class name, followed by the dot (.) operator. Now to some other programmer using our Car class, they aren’t going to see any difference functionally.

04:18 We can still perform the same actions that we did when it was just a single Car class. So, we’re going to import car and I can say my_car = car.Car(), since it was still in the same file named car. My silver

04:40 2018 Ford Fusion.

04:45 And if I want to know what color it is, I can still say f"The color of my car is {my_car.color}". I know that the Car class has an attribute called .color.

05:01 I don’t particularly need to know at this point whether it’s in a superclass or a subclass. I let the internal code of how the classes are defined take care of that.

05:13 And we know that that is a property of the superclass and so we can still retrieve it.

05:22 If I want to inquire about the voltage, the subclass Car defines that through a property and we can get that as well. And if I wanted to change the voltage,

05:36 we have the setter in the property that makes that happen

05:42 and we can see that the voltage has been changed.

05:47 The fact that we use the superclass name specifically to call a superclass method—or, had we needed to, to access a superclass variable—allows us to do something in Python that we can’t do in Java, and that’s inheriting from more than one class.

06:08 And in your next lesson, you will see how that is implemented.

Avatar image for Andy Nagai

Andy Nagai on Dec. 10, 2023

You can also call parent constructor like this:

super().__init__(color, model)

Become a Member to join the conversation.