From the course: Programming Foundations: Real-World Examples
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Inheriting classes - Python Tutorial
From the course: Programming Foundations: Real-World Examples
Inheriting classes
- 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.