Subclasses

We're going to learn about subclassing (or inheritance) in Python

We'll cover the following...

The real power of classes becomes apparent when you get into subclasses. You may not have realized it, but we’ve already created a subclass when we created a class based on object. In other words, we subclassed object. Now because object isn’t very interesting, the previous examples don’t really demonstrate the power of subclassing. So let’s subclass our Vehicle class and find out how all this works.

Press + to interact
class Car(Vehicle):
"""
The Car class
"""
#----------------------------------------------------------------------
def brake(self):
"""
Override brake method
"""
return "The car class is breaking slowly!"
if __name__ == "__main__":
car = Car("yellow", 2, 4, "car")
print(car.brake())
# 'The car class is breaking slowly!'
print(car.drive())
# "I'm driving a yellow car!"

For this example, we subclassed our Vehicle class. You ...