PYTHON INHERITANCE

In this article, you will learn about Python inheritance. You will gain insight on how a class is derive from another class inheriting its features.

inheritance

reference :https://www.besanttechnologies.com

Inheritance in Python

Inheritance, as the name suggests, is a process of inheriting features.

in  addition ,It is a technique of building new class call derive class from the existing class call a base class by inheriting the features of the base class. The features include different attributes and behavioral methods.

therefore ,In inheritance, the child class acquires the properties . it can access all the data members and functions define in the parent class. A child class can also provide its specific implementation to the functions of the parent class.

Inheritance is one of the powerful features of OOP. In the derive class, we can add extra features . also can override the data members and methods from the parent.

For example, if the vehicle is the parent class, then cars, buses, and trucks are the derived classes which have inherited all the features of parent class vehicle with many more additional features as well.

Syntax

class derived_class_name(base_class_name):

      ##Body of the derived class

 Example

Let’s take Vehicle as a parent class from which we will derive a class Category. Category class will inherit the features of parent class Vehicle and also invoke the function from the parent class.

class Vehicle:

#parent class “Parent Class”

        def __init__(self, price):

             self.price = price

     def display(self):

            print (‘Price = $’,self.price)

 class Category(Vehicle):

#derived class “Child/Derived class”

               def __init__(self, price, name):

                     Vehicle.__init__(self, price)

                       self.name = name

             def disp_name(self):

                  print (‘Vehicle = ‘,self.name)

obj = Category(1200, ‘BMW’)

obj.disp_name() obj.display()

Output

Vehicle = BMW

Price = $1200

Python isinstance( ) and issubclass( )

Python 

issubclass()

 or 

isinstance()

 functions are used to check the relationship of two classes and instances.

  • issubclass( ) is used to check class inheritance. For example, 
    insubclass(a, b)

     will return true only if 

    a

     is the subclass of 

    b

    .

example :

>>> issubclass(Category, Vehicle)

>>>True

>>> issubclass(Vehicle, Category) #Vehicle is not subclass of Category

>>>False

>>> issubclass(1, int) #1 belongs to class int

>>>True

  • isinstance( ) is used to check instance of a class. For example, 
    isinstance(obj, A)

     will return true only if 

    obj

     is the instance of 

    class A

     or of any class derived from 

    A

    .

example :

>>> isinstance(obj, Vehicle)

>>>True

>>> isinstance(obj, Category)

>>>True

Overriding methods in Python inheritance

Sometimes we need to change the functional operation of a method defined in the base class. Instead of changing the method in base class, we can define that same function in the derived class and change the codes. The base class function is overridden by the derived class function.

To override a method in the base class, we must define a new method with same name and same parameters in the derived class.

 example:

class A:

#parent class “Parent Class”

     def display(self):

               print (‘This is base class.’)

class B(A):

#derived class “Child/Derived class”

      def display(self):

             print (‘This is derived class.’)

obj = B() obj.display()

Output

This is derived class.

Note that the base 

class A

 and derived 

class B

 had the same method 

display( )

. But when we call the function with the instance of 

class B

, the function in the base class with the same name is overridden by the function in the derived class.

Types of Inheritance in Python

In the last tutorial we learned about Inheritance and how a child class can inherit a parent class to utilise its properties and functions.

What if a class want to inherit more than one class? Or it it possible to inherit a class, which already inherits some other class? To answer these questions, lets see the different types of Inheritance.

In Python, there are two types of Inheritance:

  1. Multiple Inheritance
  2. Multilevel Inheritance

Multiple Inheritance

Multiple Inheritance means that you’re inheriting the property of multiple classes into one. In case you have two classes, say 

A

 and 

B

, and you want to create a new class which inherits the properties of both 

A

 and 

B

, then:

<span class="token keyword">class</span> <span class="token class-name">A</span><span class="token punctuation">:</span>

<span class="token comment" spellcheck="true"># variable of class A</span>

    <span class="token comment" spellcheck="true"># functions of class A</span>

 

class B:

    <span class="token comment" spellcheck="true"># variable of class A</span>

    <span class="token comment" spellcheck="true"># functions of class A</span>

 

class C(A, B):

    <span class="token comment" spellcheck="true"># class C inheriting property of both class A and B</span>

<span class="token comment" spellcheck="true"># add more properties to class C</span>

So just like a child inherits characteristics from both mother and father, in python, we can inherit multiple classes in a single child class.

Multilevel Inheritance

In multilevel inheritance, we inherit the classes at multiple separate levels. We have three classes 

A

B

 and 

C

, where 

A

 is the super class, 

B

 is its sub(child) class and 

C

 is the sub class of 

B

.

<span class="token keyword">class</span> <span class="token class-name">A</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># properties of class A</span>
<span class="token keyword">class</span> <span class="token class-name">B</span><span class="token punctuation">(</span>A<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># class B inheriting property of class A</span>
    <span class="token comment" spellcheck="true"># more properties of class B</span>
 

class C(B):

    <span class="token comment" spellcheck="true"># class C inheriting property of class B</span>
    <span class="token comment" spellcheck="true"># thus, class C also inherits properties of class A</span>
    <span class="token comment" spellcheck="true"># more properties of class c</span>

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

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’
The basic idea of data abstraction is to structure programs so that they operate on abstract data. That is, our programs should use data in such a way as to make as few assumptions about the data as possible. At the same time, a concrete data representation is defined as an independent part of the program.Data abstraction is similar in character to functional abstraction. When we create a functional abstraction, the details of how a function is implemented can be suppressed, and the particular function itself can be replaced by any other function with the same overall behavior. In other words, we can make an abstraction that separates the way the function is used from the details of how the function is implemented. Analogously, data abstraction isolates how a compound data value is used from the details of how it is constructed.

%d bloggers like this: