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.

Overriding inherited methods

Overriding inherited methods - Python Tutorial

From the course: Programming Concepts for Python

Overriding inherited methods

- Nowadays, not all cars run on gasoline, some are electric. Electric cars have a radio and windows, so they could inherit those methods from the car class. An electric car also has a color and manufacturer. This is a white Nissan, so it should inherit those data fields from the vehicle class as well. Now, this is where I run into a bit of a problem. The drive method for the vehicle class uses gas, but my electric car doesn't. It uses electricity so that drive method doesn't make sense, to create an electric car subclass that inherits from the car and vehicle classes, I need to replace that gas powered drive method from the vehicle class with a specially designed eco-friendly, electric powered drive method. Let's revisit the garage full of classy vehicles from the previous video, which defines a base class for a vehicle and two subclasses for a car and a motorcycle. Now let's create a new class for electric cars named…

Contents