Inheritance is a tricky concept, so let’s go through it step by step.
Inheritance is the process by which one class takes on the attributes and methods of another, and it’s used to express an is-a relationship. For example, a Panda is a bear, so a Panda class could inherit from a Bear class. However, a Toyota is not a Tractor, so it shouldn’t inherit from the Tractor class (even if they have a lot of attributes and methods in common). Instead, both Toyota and Tractor could ultimately inherit from the same Vehicle class.
Check out the code in the editor. We’ve defined a class,
Customer, as well as a
ReturningCustomer class that inherits from
Customer. Note that we don’t define the
display_cart method in the body of
ReturningCustomer, but it will still have access to that method via inheritance. Click Run to see for yourself!