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

Multiple Inheritance

00:00 Welcome to your next lesson in Object-Oriented programming in Python versus Java. In your last lesson, we looked at how Python implements inheritance. In this lesson, you’re going to see how multiple inheritance is implemented within Python. In Java, you can only inherit from one class and if you want to have multiple superclasses, all of the others have to be interfaces. Because when you’re within a subclass in Python to access the superclass you always have to specify the superclass name, we can allow subclasses to inherit directly from more than one superclass because each time we need a superclass item—attribute, method—we’ll be preceding it with its class name.

00:53 There won’t be any ambiguity. If two different superclasses have the same name, we’ll always be referring to it using the superclass and the dot (.) operator.

01:06 So, for example, if I define Vehicle the way I had before, but now I define a class Device—and we’ll see this class deals with the voltage, anything that can have voltage, say 12 volts, we’ll call it Devicethen we’ll want Car to inherit from Vehicle and Device.

01:25 The initializer for Car is now going to have to call the Vehicle initializer and the Device initializer. But we can do that because we can specify which superclass’s .__init__() method we’re going to call by preceding it with the class name.

01:42 So, we’ll send color and model to Vehicle’s initializer by saying Vehicle.__init__(). The voltage doesn’t need a parameter—that’s always set to 12and so we don’t need any additional parameters except the calling object to send to the Device’s initializer. And we’ll take a look at that code more closely now.

02:05 So here, all in the same file, we can define all of the classes that are related to each other in a single module, in a single Python file if we want. So here is my Vehicle—again, dealing with color and model.

02:22 Here is my class Device dealing with voltage and the property setter and deleter that go with that. And here’s my Car class with just those things that are new to Carwheels, year, cup holders.

02:39 We let the Vehicle initializer deal with color and model, and we let the Device initializer deal with voltage. And then any references to those fields from the Car class will also be passed along to the superclasses because the Car inherits all of those properties. And again, I’ve not changed the functionality of the Car class. I’ve improved its design to make use of inheritance. So again, if I create my_car

03:19 I forgot the equal sign!—

03:24 I can still find out what color it is.

03:34 I can still see what its voltage is.

03:42 My cas, oops! f"My car has"

03:50 And I can still access something specific to the Car class.

03:59 So, here’s multiple inheritance as implemented in Python. In your next lesson, we will look at types and polymorphism within Python.

Become a Member to join the conversation.