Python Inheritance

Inheritance is an important aspect of the object-oriented paradigm. Inheritance provides code reusability to the program because we can use an existing class to create a new class instead of creating it from scratch.

In inheritance, the child class acquires the properties and can access all the data members and functions defined in the parent class. A child class can also provide its specific implementation to the functions of the parent class. In this section of the tutorial, we will discuss inheritance in detail.

In python, a derived class can inherit base class by just mentioning the base in the bracket after the derived class name. Consider the following syntax to inherit a base class into the derived class.


Python Inheritance
Python Inheritance

Syntax

  1. class derived-class(base class):  
  2.     <class-suite>   

A class can inherit multiple classes by mentioning all of them inside the bracket. Consider the following syntax.

Syntax

  1. class derive-class(<base class 1>, <base class 2>, ..... <base class n>):  
  2.     <class - suite>   

Example 1

  1. class Animal:  
  2.     def speak(self):  
  3.         print("Animal Speaking")  
  4. #child class Dog inherits the base class Animal  
  5. class Dog(Animal):  
  6.     def bark(self):  
  7.         print("dog barking")  
  8. d = Dog()  
  9. d.bark()  
  10. d.speak()  

Output:

dog barking
Animal Speaking

Python Multi-Level inheritance

Multi-Level inheritance is possible in python like other object-oriented languages. Multi-level inheritance is archived when a derived class inherits another derived class. There is no limit on the number of levels up to which, the multi-level inheritance is archived in python.


Python Inheritance
Python Inheritance

The syntax of multi-level inheritance is given below.

Syntax

  1. class class1:  
  2.     <class-suite>   
  3. class class2(class1):  
  4.     <class suite>  
  5. class class3(class2):  
  6.     <class suite>  
  7. .  
  8. .  

Example

  1. class Animal:  
  2.     def speak(self):  
  3.         print("Animal Speaking")  
  4. #The child class Dog inherits the base class Animal  
  5. class Dog(Animal):  
  6.     def bark(self):  
  7.         print("dog barking")  
  8. #The child class Dogchild inherits another child class Dog  
  9. class DogChild(Dog):  
  10.     def eat(self):  
  11.         print("Eating bread...")  
  12. d = DogChild()  
  13. d.bark()  
  14. d.speak()  
  15. d.eat()  

Output:

dog barking
Animal Speaking
Eating bread...

Python Multiple inheritance

Python provides us the flexibility to inherit multiple base classes in the child class.


Python Inheritance
Python Inheritance

The syntax to perform multiple inheritance is given below.

Syntax

  1. class Base1:  
  2.     <class-suite>  
  3.   
  4. class Base2:  
  5.     <class-suite>  
  6. .  
  7. .  
  8. .  
  9. class BaseN:  
  10.     <class-suite>  
  11.   
  12. class Derived(Base1, Base2, ...... BaseN):  
  13.     <class-suite>  

Example

  1. class Calculation1:  
  2.     def Summation(self,a,b):  
  3.         return a+b;  
  4. class Calculation2:  
  5.     def Multiplication(self,a,b):  
  6.         return a*b;  
  7. class Derived(Calculation1,Calculation2):  
  8.     def Divide(self,a,b):  
  9.         return a/b;  
  10. d = Derived()  
  11. print(d.Summation(10,20))  
  12. print(d.Multiplication(10,20))  
  13. print(d.Divide(10,20))  

Output:

30
200
0.5

The issubclass(sub,sup) method

The issubclass(sub, sup) method is used to check the relationships between the specified classes. It returns true if the first class is the subclass of the second class, and false otherwise.

Consider the following example.

Example

  1. class Calculation1:  
  2.     def Summation(self,a,b):  
  3.         return a+b;  
  4. class Calculation2:  
  5.     def Multiplication(self,a,b):  
  6.         return a*b;  
  7. class Derived(Calculation1,Calculation2):  
  8.     def Divide(self,a,b):  
  9.         return a/b;  
  10. d = Derived()  
  11. print(issubclass(Derived,Calculation2))  
  12. print(issubclass(Calculation1,Calculation2))  

Output:

True
False

The isinstance (obj, class) method

The isinstance() method is used to check the relationship between the objects and classes. It returns true if the first parameter, i.e., obj is the instance of the second parameter, i.e., class.

Consider the following example.

Example

  1. class Calculation1:  
  2.     def Summation(self,a,b):  
  3.         return a+b;  
  4. class Calculation2:  
  5.     def Multiplication(self,a,b):  
  6.         return a*b;  
  7. class Derived(Calculation1,Calculation2):  
  8.     def Divide(self,a,b):  
  9.         return a/b;  
  10. d = Derived()  
  11. print(isinstance(d,Derived))  

Output:

True

Method Overriding

We can provide some specific implementation of the parent class method in our child class. When the parent class method is defined in the child class with some specific implementation, then the concept is called method overriding. We may need to perform method overriding in the scenario where the different definition of a parent class method is needed in the child class.

Consider the following example to perform method overriding in python.

Example

  1. class Animal:  
  2.     def speak(self):  
  3.         print("speaking")  
  4. class Dog(Animal):  
  5.     def speak(self):  
  6.         print("Barking")  
  7. d = Dog()  
  8. d.speak()  

Output:

Barking

Real Life Example of method overriding

  1. class Bank:  
  2.     def getroi(self):  
  3.         return 10;  
  4. class SBI(Bank):  
  5.     def getroi(self):  
  6.         return 7;  
  7.   
  8. class ICICI(Bank):  
  9.     def getroi(self):  
  10.         return 8;  
  11. b1 = Bank()  
  12. b2 = SBI()  
  13. b3 = ICICI()  
  14. print("Bank Rate of interest:",b1.getroi());  
  15. print("SBI Rate of interest:",b2.getroi());  
  16. print("ICICI Rate of interest:",b3.getroi());  

Output:

Bank Rate of interest: 10
SBI Rate of interest: 7
ICICI Rate of interest: 8

Data abstraction in python

Abstraction is an important aspect of object-oriented programming. In python, we can also perform data hiding by adding the double underscore (___) as a prefix to the attribute which is to be hidden. After this, the attribute will not be visible outside of the class through the object.

Consider the following example.

Example

  1. class Employee:  
  2.     __count = 0;  
  3.     def __init__(self):  
  4.         Employee.__count = Employee.__count+1  
  5.     def display(self):  
  6.         print("The number of employees",Employee.__count)  
  7. emp = Employee()  
  8. emp2 = Employee()  
  9. try:  
  10.     print(emp.__count)  
  11. finally:  
  12.     emp.display()  

Output:

The number of employees 2
AttributeError: 'Employee' object has no attribute '__count'


;