Inheritance Example#

Let’s look at a complete example of inheritance using the bank account context.

../../_images/inheritance_example_bw.png

Parent Class#

The parent class is BaseAccount which provides common methods and attributes for a bank account.

It defines three methods:

  1. A constructor, __init__, which sets common attributes of a bank account

  2. deposit which increases the balance by a given amount

  3. withdraw which decreases the balance by a given amount

Child Class#

The child class is SavingsAccount, which inherits from BaseAccount and adds a new method get_earned_interest.

We’ve added one more attribute interest_rate to the child class, which is initialised in the constructor.

Note

Notice that because we defined a constructor we’ve had to manually initialise the attributes required by the parent class. How annoying! We’ll sort this out shortly.

Creating and Using Instances#

We can create instances of a child class without reference to the parent class as shown in the example

batman_account = SavingsAccount("689716", "62228626", 19.39, "Bruce Wayne")

Notably on the child instances since deposit and withdraw are not define the parent’s method is used e.g.

# Deposit $100.0
batman_account.deposit(100)

# Withdraw $10.0
batman_account.withdraw(10)
example.py#
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):
        self.bsb_number = bsb_number
        self.account_number = account_number
        self.balance = balance
        self.holder_name = holder_name
        self.interest_rate = interest_rate

    def get_earned_interest(self):
        return self.interest_rate * self.balance


# Create an account
batman_account = SavingsAccount("689716", "62228626", 19.39, "Bruce Wayne", 0.0235)

# Deposit $100.0
batman_account.deposit(100)

# Withdraw $10.0
batman_account.withdraw(10)

# Print earned interest
print("Earned interest: ${}".format(batman_account.get_earned_interest()))
Output
Earned interest: $2.570665
Code Challenge: Bombs Away!

The space fleet is expanding with new attack ships that will escort the other ships safely around the galaxy. For safety reasons the ships will be tested without any torpedos on board.

../../_images/bombs_away.png

Write a child class of Spaceship called AttackShip. These new attack ships are equipped with the ability to fire off torpedo rounds.

AttackShip Specifications

  • Inherit from the provided SpaceShip class

  • Adds a fire_round method with:

    • Parameters:

      • self

    • Actions:

      • Print "Bombs Away!"

    • Returns:

      • None (no return statement required)

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



xwing = AttackShip("X Wing", 500)
xwing.refuel(500)  # Fill 'er up!
xwing.fire_laser()
print(xwing.fuel_weight)
Solution

Solution is locked