2.5. Hierarchical Inheritance#
A parent class can have any number of child classes, which allows us to create a class hierarchy.
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.
![]()
Add a child class of
SpaceshipcalledInfiniteCargoShipthat is used to transport a potentially infinite amount of bulk cargo.The InfiniteCargoShiphas:
an attribute
cargo_volumeinitialised to0which tracks how much bulk cargo is on boarda
load_cargomethod which acceptsvolumeparameter and increases the cargo volumean
unload_cargomethod which reduces thevolumeby a given amount down to zero. this method returns how much volume was actually unloaded.InfiniteCargoShip Specifications
Inherit from the provided
SpaceshipclassAdditional attributes:
cargo_volumeas described above
__init__method with
Parameters:
self
name
fuel_capacityActions:
Call the Spaceship constructor to set common attributes with the same name
Set the
cargo_volumeattribute to0
load_cargomethod with
Parameters
self
volumeActions
Increase the
cargo_volumeby the given volume
unload_cargomethod with
Parameters
self
volumeActions
Reduce the
cargo_volumeby the given volume down to0.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
