From the course: Programming Concepts for Python
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Class inheritance - Python Tutorial
From the course: Programming Concepts for Python
Class inheritance
- 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 were manufactured by somebody, so I'll create another data field to store that information. And 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 I'll define a drive method, and when I use the drive method, it'll…