Join Barron Stone for an in-depth discussion in this video Inheriting classes, part of Programming Foundations: Real-World Examples.
- When creating new classes, we can pass on attributes and methods from other existing classes through a process called inheritance. This is useful when creating a new class which represents a specific subset of a previously defined more generic class. To demonstrate this concept, let's go out to the garage. Say I create a class called vehicle which contains attributes and methods that are common to all types of vehicles. All vehicles can be described in terms of color, so my class will have a color attribute.
Also, all vehicles are manufactured by somebody so I'll create another data field to store that information. Finally, although it may not be the case for much longer, my vehicles run on gasoline so I'll need a data field for the level of fuel in the tank. As far as methods go, for any vehicle to be useful, I need to be able to drive it. So we'll define a drive method. And when I use the drive method, it'll act upon that vehicle by decreasing the amount of gas in the tank. Now, this vehicle class that I just described is very generic because it needs to apply to all vehicles.
So what can I do if I need to describe some methods that only apply to a specific type of vehicle like a car? Well, I can just create a subclass. I'll call this class car and say that it inherits from the vehicle class. That means when I created the car object, it'll have a color and manufacturer as defined in the vehicle class. So this car object here is a red Mercedes with a full tank of gas. And the car will also inherit the drive method described by the vehicle class. Now, in addition to those three inherited attributes from the vehicle class, I can also add additional methods that are specific to the car class.
I can define methods to roll down the window or turn on the radio. These two methods are defined within the car class because they're specific to a car and don't necessarily apply to other types of vehicles, like a motorcycle. It doesn't make sense for the motorcycle class to have a method for opening the windows because, well, the windows are always open on a motorcycle. But it does make sense for the motorcycle to have the color and manufacturer data. My silver Harley can inherit the gas and drive methods as well.
A new class called motorcycle can inherit from the vehicle class so it gets those four things. Then in addition to the inherited data fields and methods, I can also describe some motorcycle specific things like putting on my helmet. Here, I have the start_03_01_class_inheritance script from the exercise files and it contains a garage full of classy vehicles. I have three class definitions here. The first is the Vehicle class which contains the attributes that are common to all vehicles.
It has a constructor init method which takes the color and manufacturer of the vehicle and then assigns those values to internal variables. It also creates an object called gas and sets it to 4 which represents a full tank of gas. It defaults to 4 because when you buy a new vehicle it's always nice to leave the lot with a full tank of gas. The Vehicle class also has a method called drive. It checks to see if there's gas in the tank, and if so, it uses some of that gas by decreasing the gas variable and prints a message saying that our vehicle goes vrooom.
If there's no gas left in the tank, then the message says that the vehicle sputters out of gas. The next class definition is for a car. The Car class inherits from the Vehicle class because it has Vehicle in parentheses here. That's the Python syntax for inheriting classes. You'll notice that I didn't define an init constructor method for the Car class, I don't need to because it already has those because it inherited them from the Vehicle class along with the drive method. The Car class has two unique methods.
One to turn on the radio to hear some rocking tunes and the other to open up the windows so I can get some fresh air. Both of these methods simulate their actions by printing out the corresponding messages. And finally, in the last class, Motorcycle, it also inherits from the Vehicle class. But motorcycles have their own unique method for putting on a helmet. When I call the helmet method, it'll print out a message letting me know that I'm nice and safe. I'll run this script in IDLE by going to Run, Run Module, bringing up the Python interactive shell so it can create some cars and motorcycles.
I can create a new car object called my_car using the Car constructor method to make a red Mercedes. Next, I'll create a motorcycle object called my_mc by using the Motorcycle constructor method to create a silver Harley. Remember, both the Car and Motorcycle classes has similar constructor methods because they both inherited the init method from the Vehicle class. Now, that I have a car and motorcycle, let's take them out for a test drive.
I'll call the drive method on my car which prints out a message saying that the red Mercedes goes vrooom! Now, it's time to take the motorcycle for a ride. When I call the drive method on the motorcycle, I get a slightly different message saying that the silver Harley goes vrooom. The Motorcycle and Car classes are using those data fields for color and manufacturer that were defined in the Vehicle class to create the messages in the drive method. Those data fields have different values for the car and motorcycle objects I created, the car is a red Mercedes and the motorcycle is a silver Harley.
So the messages printed by the drive method vary accordingly. I'm having so much fun, I'll think I'll keep riding a bike. So I'll call the drive method a few more times on my motorcycle. Woo-hoo! Oh, looks like I've taken a few too many twisties as the silver Harley eventually sputters out of gas. drive keeps track of the amount of gas with the data field in the motorcycle object by decrementing it every time I call the drive method. And eventually that runs out. This demonstrates that the gas data field maintains its value between calls to the drive method.
Also, the car and motorcycles are separate objects so they each have their own gas values. I've run out of gas in the motorcycle but the car still has some left so I can take it out for a drive, and again, I get the message that the red Mercedes goes vrooom. Now, to play around with some of the accessories, I could go for some rocking tunes so I'll call the radio method on my car. I also want to get some fresh air so I should roll down the windows on my car by calling the window method. That's everything special I can do with the car.
I took the motorcycle out for a test drive earlier but I forgot to put my helmet on first, shame on me. So let's do that now by calling the helmet method on my mc. Nice and safe. The joy of riding motorcycles is getting lots of fresh air so let's try opening the window on the motorcycle by calling the window method. Uh-oh, we have a problem. The Motorcycle class doesn't have a window method because it doesn't have windows. The Car class and the Motorcycle class both inherit from the Vehicle class so they both have the same drive method but only the Car class has radio and window methods and only the Motorcycle class has a helmet method.
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming