Inheritance is one of the properties of any object-oriented programming language. Inheritance allows a class to share its resource with another class without rewriting the code.
In this tutorial, we will discover how inheritance can help us in writing reusable codes in Python.
Introduction to Inheritance in Python
Inheritance in context to OOPs language is a way using which a class can reuse the properties (attributes) and behavior (function) of another class.
The class which inherits another class is called the derived or sub-class and the class which has been inherited is known as the parent or the super-class.
The following block diagram illustrates the flow of properties in Inheritance:
Here the arrow represents the flow of resources. This means the derived class has access to the attributes and behaviors of the base class.
Syntax to inherit a class:
Similar to other OOP languages, inheritance in Python works by extending one class to another.
To inherit or extend a class, we provide the base class’s name inside the parenthesis of the derived class declaration as follows:
class BaseClass:
#statements
class DerivedClass(BaseClass):
#statements
Here the DerivedClass
is inheriting a single parent class BaseClass
.
When a Class should inherit Another Class?
Though we can inherit any class in Python, we should refrain from doing so and only inherit when there is a ‘IS A‘ relationship between two classes.
For example, Dog is an Animal (a dog has the properties of an animal), therefore the Dog class can extend to Animal class in programming.
Let’s see some examples for a clear understanding.
Example 1: Dog is an Animal
#base Class
class Animal:
def __init__(self):
print("I am an Animal")
def eat(self):
print("I can eat")
#derived Class
class Dog(Animal):
def __init__(self):
super().__init__() #to call base class __init__ method
print("I am a Dog")
def sound(self):
print("I can Bark")
dog = Dog()
dog.eat()
dog.sound()
Output:
I am an AnimalI am a Dog
I can eat
I can Bark
The Super() method returns a proxy object of the Base class that allows using base class’s methods in the derived class.
Since a Dog is an Animal, so we have inherited the Animal
class and is using its eat
method in the Dog
class.
As you can see, without any error we are able to call eat()
method on the Dog’s object, which proves that we have successfully inherited the Animal
class’s resources to the Dog
class.
Let’s see one more example of inheritance.
Example 2: Saving Account is a Bank Account
class Account:
balance = 0
def __init__(self, balance):
self.balance = balance
def check_balance(self):
print("Current Balance: ",self.balance)
def deposit(self,amt):
self.balance += amt
print(amt," deposit Successful")
def withdraw(self, amt):
if(amt > self.balance):
print("Insufficient Balance")
return
self.balance -= amt
print(amt," withdraw Successful")
class SavingAccount(Account):
rate = 6.9
def __init__(self, balance):
super().__init__(balance)
def interest(self, months):
print("Interest: %.2f" %(self.balance * (self.rate/100) * months))
account = SavingAccount(800)
account.deposit(200)
account.withdraw(300)
account.check_balance()
account.interest(5)
Output:
200 deposit Successful300 withdraw Successful
Current Balance: 700
Interest: 241.50
In this example, we have inherited the Account
class because SavingAccount
is also an Account
.
All the properties and methods of the Account
class is also valid for SavingAccount
, thus we can reuse them using the inheritance in Python.
Types of Inheritance in Python:
Till now we have been inheriting only a single base class in the derived class. However, we can extend more classes as Python allows multiple inheritance.
We can even inherit a derived class into another derived class, as the level of inheritance is only limited in our minds.
Depending on the degree and level, inheritance in Python can be categorized into the following types:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Hope now you will feel a little bit confident in writing inheritance related programs in Python. If you have any doubts then comment below.
I liked your simple straight forward examples 🙂