1. Class inheritance
Welcome back! Now that you got the basics of classes and instances down, we'll get to the essence of OOP.
2. Code reuse
Object-oriented programming is fundamentally about code reuse.
There are millions of people out there writing code, so there's a good chance
3. Code reuse
that someone has already written code that solves a part of your problem!
Modules like numpy or pandas are a great tool that allows you to use code written by other programmers. But what if that code doesn't match your needs exactly? For example, you might want to modify the to_csv method of a pandas DataFrame to adjust the output format. You could do that by importing pandas and writing a new function, but it will not be integrated into the DataFrame interface. OOP will allow you to keep interface consistent while customizing functionality.
4. Code reuse
You will also often find yourself reusing your own code over and over. For example, when building a website with a lot of graphical elements like buttons and check boxes, no matter what the element is, the basic functionality is the same: you need to be able to draw it and click on it. There are a few details that differ based on the type of the element, but a lot of the code will be the repeated. Should you copy-paste the basic code for draw and click for every new element?
5. Code reuse
Wouldn't it be better to have a general data structure like GUIelement that implements the basic draw and click functionality only once?
6. Inheritance
We can accomplish this with inheritance. Class inheritance is mechanism by which we can define a new class that gets all the the functionality of another class plus maybe something extra without re-implementing the code.
7. Example hierarchy
Let's say you have a basic bank account class that has a balance attribute and a withdraw method.
In your company, you work with several types of accounts.
8. Example hierarchy
For example, a SavingsAccount also has an interest rate and a method to compute interest, but it will also still have a balance, and you definitely should be able to withdraw from it. By inheriting methods and attributes of SavingsAccount from BankAccount, you'll be able to reuse the code you already wrote for the BankAccount class.
9. Example hierarchy
You could have a CheckingAccount class, that also has a balance, and a withdraw method, but maybe that method is slightly different:
10. 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 -- again, without rewriting it.
11. Implementing class inheritance
How do we implement this? Declaring a class that inherits from another class is very straightforward: you simply add parentheses after the class name, and then specify the class to inherit from. Here, we define a rudimentary BankAccount class and a seemingly empty SavingsAccount class inherited from it.
12. Child class has all of the the parent data
"Seemingly" because SavingsAccount actually has exactly as much in it as the BankAccount class.
For example, we can create an object -- even though we did not define a constructor -- and we can access the balance attribute and the withdraw method from the instance of SavingsAccount, even though these features weren't defined in the new class.
13. Inheritance: "is-a" relationship
That's because inheritance represents "is-a" relationship: a savings account is a bank account, just with some extra features.
This isn't just theoretical -- that's how Python treats it as well.
Calling isinstance function on a savingsaccount object shows that Python treats it like an instance of both savingsaccount and BankAccount classes, which is not the case for a generic BankAccount object.
Right now, though, this class doesn't have anything that the original account class did not have.
14. Let's practice!
We'll start adding features and customizing the class the next video, but for now, head over to the exercises to test your understanding of inheritance.