Python Inheritance in Practice
Inheritance in practice is a subclass that reuses a general class. The subclass adds its own detail and calls super() for the shared part.
A general class first
Account holds an owner and a balance. deposit() adds money and returns the new balance.
class Account:
def __init__(self, owner):
self.owner = owner
self.balance = 0
def deposit(self, amount):
self.balance = self.balance + amount
return self.balance
account = Account("Ada")
print(account.deposit(100))A subclass adds its own detail
SavingsAccount calls super() for owner and balance. It adds a rate and an interest method.
class Account:
def __init__(self, owner):
self.owner = owner
self.balance = 0
def deposit(self, amount):
self.balance = self.balance + amount
return self.balance
class SavingsAccount(Account):
def __init__(self, owner, rate):
super().__init__(owner)
self.rate = rate
def add_interest(self):
self.balance = self.balance + self.balance * self.rate / 100
return self.balance
saving = SavingsAccount("Ada", 10)
saving.deposit(100)
print(saving.add_interest())The subclass keeps the parent method
SavingsAccount has no deposit of its own. It uses the parent deposit after interest is added.
class Account:
def __init__(self, owner):
self.owner = owner
self.balance = 0
def deposit(self, amount):
self.balance = self.balance + amount
return self.balance
class SavingsAccount(Account):
def __init__(self, owner, rate):
super().__init__(owner)
self.rate = rate
def add_interest(self):
self.balance = self.balance + self.balance * self.rate / 100
return self.balance
saving = SavingsAccount("Ada", 10)
saving.deposit(100)
saving.add_interest()
print(saving.deposit(20))Example
class Account:
def __init__(self, owner):
self.owner = owner
self.balance = 0
def deposit(self, amount):
self.balance = self.balance + amount
return self.balance
def __str__(self):
return self.owner + ": " + str(self.balance)
class SavingsAccount(Account):
def __init__(self, owner, rate):
super().__init__(owner)
self.rate = rate
def add_interest(self):
self.balance = self.balance + self.balance * self.rate / 100
return self.balance
plain = Account("Bob")
plain.deposit(50)
saving = SavingsAccount("Ada", 10)
saving.deposit(100)
saving.add_interest()
print(plain)
print(saving)
print("Ada after a deposit:", saving.deposit(20))Key points
- Subclass: a class that extends a general class
- Use super().__init__() to run the parent __init__ first
- The subclass keeps the parent methods and adds its own
- A subclass method can replace a parent method with its own version
- Put shared data in the parent class, not in each subclass
In this example, Account stores an owner and a balance, with deposit() and __str__ for both kinds of account. SavingsAccount calls super().__init__() to set owner and balance, then stores its own rate. Ada deposits 100, interest adds 10, and the balance prints as 110.0. A further deposit of 20 uses the parent method and brings the balance to 130.0, while Bob stays at 50.
Note
super() runs the parent version. The subclass then adds only what is new.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does super().__init__(owner) do in SavingsAccount?