Learn Python – Python Inheritance- Basic and advance

Inheritance is an essential element of the object-oriented paradigm. Inheritance presents code reusability to the program because we can use an existing classification to create a new type as a substitute of creating it from scratch.

In inheritance, the child class acquires the residences and can get admission to all the facts members and features described in the guardian class. A child category can also grant its precise implementation to the features of the mother or father class. In this part of the tutorial, we will talk about inheritance in detail.

In python, a derived classification can inherit base type by means of simply mentioning the base in the bracket after the derived category name. Consider the following syntax to inherit a base classification into the derived class.

Syntax

class derived-class(base class):  
    <class-suite>   

A class can inherit multiple instructions by means of citing all of them internal the bracket. Consider the following syntax.

Syntax

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

Example 1

class Animal:  
    def speak(self):  
        print("Animal Speaking")  
#child class Dog inherits the base class Animal  
class Dog(Animal):  
    def bark(self):  
        print("dog barking")  
d = Dog()  
d.bark()  
d.speak()  

Output:

dog barking
Animal Speaking

Python Multi-Level inheritance

Multi-Level inheritance is viable in python like different object-oriented languages. Multi-level inheritance is archived when a derived classification inherits another derived class. There is no restriction on the number of stages up to which, the multi-level inheritance is archived in python.

The syntax of multi-level inheritance is given below.

Syntax

class class1:  
    <class-suite>   
class class2(class1):  
    <class suite>  
class class3(class2):  
    <class suite>  
.  
.  

Example

class Animal:  
    def speak(self):  
        print("Animal Speaking")  
#The child class Dog inherits the base class Animal  
class Dog(Animal):  
    def bark(self):  
        print("dog barking")  
#The child class Dogchild inherits another child class Dog  
class DogChild(Dog):  
    def eat(self):  
        print("Eating bread...")  
d = DogChild()  
d.bark()  
d.speak()  
d.eat()  

Output:

dog barking
Animal Speaking
Eating bread...

Python Multiple inheritance

Python provides us the flexibility to inherit more than one base training in the child class.

The syntax to perform more than one inheritance is given below.

Syntax

class Base1:  
    <class-suite>  
  
class Base2:  
    <class-suite>  
.  
.  
.  
class BaseN:  
    <class-suite>  
  
class Derived(Base1, Base2, ...... BaseN):  
    <class-suite>  

Example

class Calculation1:  
    def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(d.Summation(10,20))  
print(d.Multiplication(10,20))  
print(d.Divide(10,20))  

Output:

30
200
0.5

The issubclass(sub,sup) method

The issubclass(sub, sup) technique is used to test the relationships between the particular classes. It returns true if the first category is the subclass of the 2d class, and false otherwise.

Consider the following example.

Example

class Calculation1:  
    def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(issubclass(Derived,Calculation2))  
print(issubclass(Calculation1,Calculation2))  

Output:

True
False

The isinstance (obj, class) method

The isinstance() technique is used to take a look at the relationship between the objects and classes. It returns true if the first parameter, i.e., obj is the occasion of the second parameter, i.e., class.

Consider the following example.

Example

class Calculation1:  
    def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(isinstance(d,Derived))  

Output:

True

Method Overriding

We can provide some unique implementation of the parent category technique in our infant class. When the parent class method is defined in the toddler category with some particular implementation, then the idea is known as technique overriding. We can also want to operate approach overriding in the scenario the place the distinct definition of a parent classification technique is wanted in the toddler class.

Consider the following example to operate approach overriding in python.

Example

class Animal:  
    def speak(self):  
        print("speaking")  
class Dog(Animal):  
    def speak(self):  
        print("Barking")  
d = Dog()  
d.speak()  

Output:

Barking

Real Life Example of method overriding

class Bank:  
    def getroi(self):  
        return 10;  
class SBI(Bank):  
    def getroi(self):  
        return 7;  
  
class ICICI(Bank):  
    def getroi(self):  
        return 8;  
b1 = Bank()  
b2 = SBI()  
b3 = ICICI()  
print("Bank Rate of interest:",b1.getroi());  
print("SBI Rate of interest:",b2.getroi());  
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 necessary aspect of object-oriented programming. In python, we can also function information hiding by adding the double underscore (___) as a prefix to the attribute which is to be hidden. After this, the attribute will not be seen outside of the type through the object.

Consider the following example.

Example

class Employee:  
    __count = 0;  
    def __init__(self):  
        Employee.__count = Employee.__count+1  
    def display(self):  
        print("The number of employees",Employee.__count)  
emp = Employee()  
emp2 = Employee()  
try:  
    print(emp.__count)  
finally:  
    emp.display()  

Output:

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