Hierarchical Inheritance

Hierarchical Inheritance#

A parent class can have any number of child classes, which allows us to create a class hierarchy.

../../_images/hierarchical_inheritance.png

The syntax for this remains the same as before

class ParentClassName:

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

class ChildClassName1(ParentClassName):

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

class ChildClassName2(ParentClassName):

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

Example

In the example below the SavingsAccount and FeeChargingAccount classes inherit from the BaseAccount

class BaseAccount:

    def __init__(self, bsb_number, account_number, balance, holder_name):
        self.bsb_number = bsb_number
        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 SavingsAccount(BaseAccount):

    def __init__(self, bsb_number, account_number, balance, holder_name, interest_rate):
        super().__init__(bsb_number, account_number, balance, holder_name)
        self.interest_rate = interest_rate
class FeeChargingAccount(BaseAccount):

    def withdraw(self, amount):
        fee = 2.50
        super().withdraw(amount + fee)  # Calls the method defined on line 8
Code Challenge: Infinite Cargo Ship

Thanks to advancements in quantum technology we can now store an infinite amount of a single type of bulk cargo in spaceships. You’ve decided to add these special ships to your fleet.

../../_images/cargo_ship.png

Add a child class of Spaceship called InfiniteCargoShip that is used to transport a potentially infinite amount of bulk cargo. The InfiniteCargoShip has:

  • an attribute cargo_volume initialised to 0 which tracks how much bulk cargo is on board

  • a load_cargo method which accepts volume parameter and increases the cargo volume

  • an unload_cargo method which reduces the volume by a given amount down to zero. this method returns how much volume was actually unloaded.

InfiniteCargoShip Specifications

  • Inherit from the provided Spaceship class

  • Additional attributes:

    • cargo_volume as described above

  • __init__ method with

    • Parameters:

      • self

      • name

      • fuel_capacity

    • Actions:

      • Call the Spaceship constructor to set common attributes with the same name

      • Set the cargo_volume attribute to 0

  • load_cargo method with

    • Parameters

      • self

      • volume

    • Actions

      • Increase the cargo_volume by the given volume

  • unload_cargo method with

    • Parameters

      • self

      • volume

    • Actions

      • Reduce the cargo_volume by the given volume down to 0.

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


freighter = InfiniteCargoShip("Galactic Freighter", fuel_capacity=800)

freighter.load_cargo(200)

freighter.unload_cargo(150)
print(freighter.cargo_volume)

unloaded_volume = freighter.unload_cargo(100)
print(unloaded_volume)

print(freighter.cargo_volume)
Solution

Solution is locked