Classes
Create a SavingsAccount Subclass
Learning objective: By the end of this lesson, students will be able to create a sub class and instantiate objects from it.
🎓 You Do
Create a SavingsAccount Class
Looking for even more practice building an object hierarchy in Python? Great!
Extend the BankAccount Class
- First, modify the existing
BankAccountclass to include an additional attribute calledhas_overdraft.- This attribute should accept a Boolean value (True or False).
- It should default to
Falseif no value is provided during instantiation. This requires using a default parameter in the class’s constructor.
- Adjust the withdraw method to check the
has_overdraftstatus.- If
has_overdraftisFalseand the withdrawal amount exceeds the balance, deny the withdrawal and return an appropriate message. - Otherwise, if
has_overdraftisTrue, deduct the amount from the balance and return the new balance.
- If
Create a SavingsAccount Class
- Next, create a new
SavingsAccountclass that subclasses theBankAccountclass.- This new class should specialize the withdraw method. In the
SavingsAccountclass, override the withdraw method so that it no longer accepts any arguments, does not change the account balance, and returns the message"No withdrawals permitted".
- This new class should specialize the withdraw method. In the