Summary: Inheritance is one of the properties of Python (object-oriented programming language). Inheritance in Python enables code reusablity and as a result, the complexity of the program decreases. Let’s learn about inheritance with examples.
Introduction to Python Inheritance
Inheritance in python is a way through which we can use the property of one class into another class. This ensures the reusability of code and hence reduces the complexity of the program.
The class which inherits another class is called derived or sub class and the class which has been inherited is known as the parent or super class.
The following block diagram illustrates the flow of properties in Python Inheritance:
Similar to other OOP languages, Inheritance in Python works by extending one class to another.
class Base_Class: #statements pass class Derived_Class(Base_Class): #statements pass
To inherit or extend a class, we provide the base class name inside the parenthesis of the derived class.
It is important to note that we should only inherit from another class if there is a ‘IS A‘ relationship.
For example, Dog is an Animal (dog has the properties of an animal).
Let’s see this in action with python program.
Recommended: Class and objects in Python.
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") #Creating only Derived class object dog = Dog() #calling methods using the object dog.eat() dog.sound()
Output:I am an Animal
I am a Dog
I can eat
I can Bark
We have created an ‘Animal’ class and inherited it in the dog class.
super() method we have called the Animal class
__init__() method by Dog class
Super() method returns the reference of Base class, hence allowing us to use base class reference into any derived class.
In the driver code, we have created an object of the Dog class and invoked all the methods of both classes. As you can see, that without any error we are able to call
eat() method of the Animal class using Dog class object, which proves that we have successfully inherited the Animal class into Dog class i.e. implemented inheritance.
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 Saving_Account(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 = Saving_Account(800) account.deposit(200) account.withdraw(300) account.check_balance() account.interest(5)
Output:200 deposit Successful
300 withdraw Successful
Current Balance: 700
Hope now you will feel a little bit confident in writing inheritance related programs in python. If you have any doubts then comment below.