Get startedGet started for free

Inheritance reuses code

1. Inheritance reuses code

It’s a new sprint at PyBank. Before you continue coding, it’s time to review your progress so far.

2. Roadmap

Here, you can check how far you've come and what's still ahead - the `MortgageCalculator` class and more inheritance!

3. Review of what you've built so far

First, you created the `BasicCalculator` class, which wraps NumPy arithmetic functions. Next, you extended this to create the `FinancialCalculator` class, inheriting from `BasicCalculator` so it could use those arithmetic methods while adding a new method called `monthly_interest`. In all of this, you used inheritance. Let's dig a little deeper into how it works.

4. Inheritance Reuses Code

Inheritance helps you follow the DRY principle by reusing code from a parent class. You can also extend this by inheriting from a child class, creating a “grandchild” class, and building a flexible class hierarchy. In this project, you start with a `BasicCalculator`, extend it to a child class called `FinancialCalculator`, and further extend it to a grandchild class called `MortgageCalculator`.

5. Other types of inheritance

There are other types of inheritance. The type you're using here is multilevel inheritance, where a class inherits from another class that is itself a subclass. There are two other types you won't be using: hierarchical and multiple inheritance. In hierarchical inheritance, multiple classes inherit from the same parent class and share its methods and properties. For example, imagine you had both a `FinancialCalculator` and a new `CurrencyConversionCalculator`, both inheriting from the `BasicCalculator`. With multiple inheritance, a class inherits from two independent classes. A real-life example would be a dolphin — it's both a mammal and an aquatic animal. To summarize: multilevel inheritance involves a class inheriting from another class that's already a subclass, hierarchical inheritance occurs when multiple classes inherit from the same parent, and multiple inheritance happens when a class inherits from two or more unrelated classes.

6. Using super()

When coding a child class, it's a good practice to use the `super()` method instead of directly calling `__init__`. Using `super()` allows Python to access methods and properties from a parent class without naming it directly. This approach clarifies your code by signaling a call to the superclass, making it easier to read and understand. In complex inheritance structures, calling a parent class's `__init__` method directly can lead to duplication and errors. The `super()` method ensures that each class's `__init__` is called once and in the correct order. It also eliminates the need to hard code the parent class name, so if you make changes to the hierarchy, you won't need to update your code everywhere You’ll use `super()` in the MortgageCalculator to take full advantage of these benefits. For instance, look at this Animal class - it acts as a blueprint for creating animals. When you create an animal, you provide a name, which is stored so you can use it later. The Dog class inherits from Animal. It then uses `super().__init__(name)` to set its attribute - the name.

7. Let's practice!

Good job learning about the different types of inheritance, and the use of `super()`!