Best Practices Designing for Inheritance and Polymorphism Polymorphism Interface
1 2 3 4 5 6 |
# Withdraw amount from each account in list_of_accounts def batch_withdraw(list_of_accounts, amount): for acct in list_of_accounts: acct.withdraw(amount) b, c, s = BankAccount(1000), CheckingAccount(2000), SavingsAccount(3000) batch_withdraw([b,c,s]) # uses bankaccount.withdraw, checking, Savings |
batch_withdraw() doesn’t need to check the object to know which withdraw() to call Liskov substitution principle Base class should be interchangeable with any of its subclasses without altering any properties of the program Wherever BankAccout woorks –> CheckingAccount should work as well Syntactically function […]