Multilevel Inheritance

Multilevel Inheritance#

Classes can inherit from each indefinitely, creating multiple levels of inheritance.

../../_images/multilevel_inheritance.png

Luckily you only need to reference the closest parent!

Syntax

class ParentClassName:

    def __init__(self, parameter_1, ..., parameter_n):
        # Set values here

class FirstChildClassName(ParentClassName):

    def __init__(self, parameter_1, ..., parameter_n):
        # Set values here

class SecondChildClassName(ChildClassName):

    def __init__(self, parameter_1, ..., parameter_n):
        # Set values here

...

class NthChildClassName(ChildClassName):

    def __init__(self, parameter_1, ..., parameter_n):
        # Set values here

Example

In the example below the PremiumCreditAccount adds rewards points functionality to a credit card class.

The PremiumCreditAccount inherits from CreditAccount and then BaseAccount. Therefore the PremiumCreditAccount class has both methods:

  • get_owed_interest

  • get_rewards_points

class BaseAccount:

    def __init__(self, account_number, balance, holder_name):
        self.account_number = account_number
        self.balance = balance
        self.holder_name = holder_name

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        self.balance -= amount
class CreditAccount(BaseAccount):

    interest_rate = 0.07  # 7% interest rate
    limit = 5000

    def get_owed_interest(self):
        return (CreditAccount.limit - self.balance) * CreditAccount.interest_rate
class PremiumCreditAccount(CreditAccount):

    def get_rewards_points(self):
        return self.balance * 0.001
Code Challenge: Shields Up

Note

For this exercise you’ll need the solution to Super > “Attack Ship”

Space pirates have started to develop their own weapons technology and have been firing at your fleet. Your top boffins have developed rechargeable shields that you can add on to your attack ships.

../../_images/shields_up.png

Add a child class of AttackShip called ShieldedAttackShip that adds:

  • a shield_level attribute used to track the strength of energy shields surrounding the ship. the shield level starts at 100.

  • a recharge method that sets the shield level back to 100

  • a damage method that reduces the shield level by a given amount down to 0

ShieldedAttackShip Specifications

  • Inherit from your AttackShip class

  • Additional attributes:

    • shield_level the amount of shields remaining

  • __init__ method with

    • Parameters:

      • self

      • name

      • fuel_capacity

      • rounds

    • Actions:

      • Call the AttackShip constructor to set common attributes

      • Set the shield_level attribute to 100.

  • damage method with

    • Parameters:

      • self

      • amount

    • Actions:

      • Reduces the shield level by the given amount down to 0.

    • Returns:

      • None (no return statement required)

  • recharge method with

    • Parameters:

      • self

    • Actions:

      • Resets the shield level to 100

Here is some code for you to start with:

class Spaceship:
    def __init__(self, name, fuel_capacity):
        self.name = name
        self.fuel_weight = 0
        self.fuel_capacity = fuel_capacity

    def refuel(self, weight):
        if self.fuel_weight + weight > self.fuel_capacity:
            self.fuel_weight = self.fuel_capacity
        else:
            self.fuel_weight += weight

    def fire_laser(self):
        if self.fuel_weight >= 10:
            self.fuel_weight -= 10


# ADD YOUR CLASS HERE


# X-Wings carry 6 torpedos
xwing = ShieldedAttackShip("X Wing", 500, 6)
xwing.refuel(500)  # Fill 'er up!

xwing.damage(20)
print(xwing.shield_level)

xwing.damage(100)
print(xwing.shield_level)
Solution

Solution is locked