|Inheritance in Python|
|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.
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 = $',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()
Vehicle = BMW Price = $1200
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( )
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
ais the subclass of
>>> 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
objis the instance of
class Aor of any class derived from
>>> 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()
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.