Inheritance
Not met yet
class ElectricBike(Bike): makes a subclass: an electric bike is a kind of bike. It inherits every method from its parent and overrides only the ones it writes itself. Inside an inherited method, self is still the subclass object, so its overrides win. isinstance(volt, Bike) is True.
Try it
main.pyOutput
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks a method inherited from the parent always calls the parent's versions of other methods, even on a subclass object.
- Thinks a subclass has none of its parent's methods unless it writes them again.
- Thinks overriding a method in a subclass changes it for the parent class too.
- Thinks an object of the parent class also counts as an object of its subclasses.