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.

Override inherited methods

Override inherited methods - Python Tutorial

From the course: Programming Foundations: Real-World Examples

Start my 1-month free trial

Override 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. Here I have start_03_02_class_inheritance open from the exercise files. Now, I'll create a new class for electric cars called eCar, which will inherit from the Car class. Since eCar inherits the Car class, it'll have the window and radio methods from it. And, since the Car class…

Contents