Python Class Inheritance
Inheritance in Python
Objectives
Implement inheritance
Describe what has been inherited from one class to another
Identify inheritance syntax
Use inheritance to reduce repetition
Use inheritance to model the world
Review
What is a method?
What is a class?
What is an instance method?
Why do we use a class?
Inheritance
Inheritance allows us to build new classes out of old classes. It allows us to extend functionality defined in a parent
class and create children
classes that extend and compartmentalize different pieces of functionality.
Inheritance models natural hierarchies that we're used to thinking about in the world. We can define one general class to model something like an Phone and then inherit
the methods and properties of the class to make new classes out of the first class, like IPhone and AndroidPhone.
When we say sub-classes, or child classes, inherit
methods and properties from a parent class we mean the child class has access to all of the functionality of it's parent, and it can define it's own functionality on top of that.
When we define a class to represent a Phone we can add the basic properties and functionality that all phones have.
All phones have a phone number
All phones can place phone calls
All phone can send text messages
After we define what a Phone is we can create classes that inherit
from the Phone class and add their own properties and functionality.
Let's define two new classes that inherit
from the Phone class. We'll make an IPhone and an AndroidPhone.
iPhones have a unique
unlock
method that accepts afingerprint
-> str as a parameter.iPhones have a unique
set_fingerprint
method that accepts afingerprint
-> str as a parameter.Android phones have a unique
set_keyboard
method that accepts akeyboard
-> str as a parameter.
Inheritance Syntax
There's two new pieces of syntax used in the code above.
Class definitions can accept a parameter specifying what class they inherit
from.
Child classes can invoke a method called
super()
to gain access tomethods defined in the parent class and execute them.
Take another look at the Phone classes to see how these pieces of syntax are used to define how the classes define their inheritance and how the super()
method is used.
Notice how the Android class doesn't repeat the code that attaches the phonenumber passed to the `_initmethod to the
self` reference. The Android class calls the parent constructor through the super method and allows the parent class to execute that default behavior.
Exercise: Write Bank Account Classes
Let's practice writing classes and using inheritance by modelling different types of Bank accounts.
Create a base BankAccount class
Bank accounts keep track of their current
balance
Bank accounts have a
deposit
methodBank accounts have a
withdraw
methodthe
deposit
method returns the balance of the account after addingthe deposited amount.
the
withdraw
method returns the amount of money that was successfullywithdrawn.
Bank accounts return
False
if someone tries to deposit or withdrawa negative amount.
Bank accounts are created with a default interest rate of 2%
Bank accounts have a
accumulate_interest
method that sets the balanceequal to the balance plus the balance times the interest rate
accumulate_interest
returns the balance of the account after calculatingthe accumulated interest
Create a ChildrensAccount class
Children's bank accounts have an interest rate of Zero.
Every time
accumulate_interest
is executed on a Child's account theaccount always gets $10 added to the balance.
Create an OverdraftAccount class
An overdraft account penalizes customers for trying to draw too much
money out of their account.
Overdraft accounts are created with an
overdraft_penalty
propertythat defaults to $40.
Customer's aren't allowed to withdraw more money than they have in their
account. If a customer tries to withdraw more than they have then the
withdraw method returns
False
and their balance is deducted only bythe amount of the
overdraft_penalty
.Overdraft accounts don't accumulate interest if their balance is below zero.
Sample Input:
Sample Output:
Last updated