Python Inheritance

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

Inheritance in Python
Inheritance syntax
Example of inheritance in Python
Python isinstance( ) and issubclass( )
Overriding methods in inheritance

Inheritance in Python

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

It is a technique of building new classes called derived classes from the existing class called a base class by inheriting the features of the base class. The features include different attributes and behavioral methods.

Look at this picture for better understanding.

python inheritance

Inheritance is one of the powerful features of OOP. In the derived class, we can add extra features and 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.

Python Inheritance Syntax

class derived_class_name(base_class_name):
  ##Body of the derived class

Python Inheritance 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.

Here is the code.

class Vehicle:   #parent class
  "Parent Class"
  def __init__(self, price):
    self.price = price
  def display(self):
    print ('Price =

Output

Vehicle = BMW
Price = $1200

[adsense1]

Here in the program, we have only instantiated the derived class Category. As the Category is the child class of Vehicle, we can use the function display() of the base class using the object of derived class.

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.
    >>> 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.
    >>> 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.

Here is an 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.



,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


[adsense1]

Here in the program, we have only instantiated the derived class Category. As the Category is the child class of Vehicle, we can use the function display() of the base class using the object of derived class.

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.
    
    
  • 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.
    
    

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.

Here is an example.


Output


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.