Get startedGet started for free

Class inheritance

1. Class inheritance

Now that we know the fundamentals of classes and instances, we'll get to the essence of OOP,

2. Code reuse

which is fundamentally about code reuse. Millions of people are writing code, so there's a good chance someone has already written code that solves a part of our problem! Packages like requests allow us to reuse code written by other developers. But what if that code doesn't match our needs exactly? For example, we might want to modify the requests.get function so that the default output of the response's content is always JSON. We could do that by importing requests and writing a new function, but it will not be integrated into the response class. OOP will allow us to keep a consistent interface while customizing functionality.

3. Code reuse

We will also often find ourselves reusing our own code repeatedly. For example, when building a website with lots of elements like buttons and checkboxes, regardless of the element, the basic functionality is the same: we need to be able to click on it and trigger an action. A few details differ based on the element type, but a lot of the code will be repeated. Should we copy-paste the code for every new element?

4. Code reuse

A better approach is to have a general data structure like GUIelement that implements the required functionality.

5. Inheritance

We can accomplish this with class inheritance. Inheritance is a mechanism by which we can define a new class that gets all the functionality of another class without re-implementing the code, allowing us to quickly build extra functionality on top!

6. Example hierarchy

Let's say we have a bank account class with a balance attribute and a withdraw method. In our company, we work with several types of accounts.

7. Example hierarchy

For example, a SavingsAccount also has an interest rate and a method to compute interest, but it will still have a balance, and we can withdraw from it. By having SavingsAccount inherit methods and attributes from BankAccount we can reuse the code we already wrote for the BankAccount class.

8. Example hierarchy

We could have a CheckingAccount class that also has a balance and a withdraw method, but maybe that method is slightly different:

9. Example hierarchy

it modifies the amount to be withdrawn to include a fee. With inheritance, we'll be able to customize the withdraw method to first adjust the amount if necessary, and then use the method from the BankAccount class without rewriting it.

10. Implementing class inheritance

We have our BankAccount class here, so how do we implement a SavingsAccount class that inherits from BankAccount? We declare a new class, but this time, we add parentheses after the class name and, inside, specify the class to inherit from. Using the pass keyword results in the SavingsAccount class having all the functionality of BankAccount. In inheritance, the class we inherit from is known as the Parent class, while the class that inherits from another is known as the Child class or subclass.

11. Child class has all of the parent data

We can test the functionality. Here, we create an object from the SavingsAccount class even though we did not define a constructor. We can access the balance attribute and use the withdraw method from the object even though these features weren't defined in the child class! This is an example of both polymorphism, which defines a unified interface, as well as inheritance, because the parent and child classes can be used in the same way!

12. Inheritance: "is-a" relationship

That's because an instance of a Child class is an instance of the Parent class too. We can confirm this by calling isinstance on a SavingsAccount object, showing that Python treats it as an instance of both the SavingsAccount and BankAccount classes. However, this is not the case for a generic BankAccount object.

13. Let's practice!

We'll add features and customize a child class in the next video, but for now, head over to the exercises to test your understanding of inheritance.