Python Tutorial Python Advanced Python References Python Libraries

Python - Inheritance



Inheritance enables a class to inherit the properties and methods from another class. The class which is being inherited is called base class or parent class. The class which inherits from another class is called derived class or child class. Inheritance provides re usability of a code and adds more features to a class without modifying it.

Create derived Class

To create a derived class, it is must to specify the base class when it is created. See the syntax below:

Syntax

#Base class
class base_class:
    statements

#Derived class
class derived_class(base_class):
    statements

Example:

In the example below, a base class called polygon is created which has two properties called length and breadth. rectangle, a derived class of polygon is also created. The derived class rectangle inherits all properties of base the class polygon.

class polygon:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth

class rectangle(polygon):
  pass

MyRectangle = rectangle(3, 5)
print(MyRectangle.length)
print(MyRectangle.breadth)

The output of the above code will be:

3
5

__init__() function of derived class

In last example, the derived class inherited the object initialization method from its base class. But it can be defined in derived class also. In such situation, __init__() function of derived class will override the __init__() function of base class when a object of derived class is created.

Example:

In the example below, __init__() function of the base class polygon and derived class square are different. polygon class requires two properties to initiate an object: length and breadth. While square class requires one property to initiate an object: side.

The __init__() function of base class polygon is overridden by __init__() function of derived class square. Due to this, object of square class requires one parameter to initialize. In order to inherit the properties and methods of class polygon, it is called inside derived class square to initialize polygon object.

class polygon:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth
  def area(self):
    return self.length * self.breadth
  
class square(polygon):
   def __init__(self, side):
      polygon.__init__(self, side, side)
    
MySquare = square(3)
print(MySquare.area())

The output of the above code will be:

9

The super() function

The above result can also be achieved using super() function. The Python super() function is used to initialize base class object in derived class (in single inheritance), hence helps derived class to inherit all the methods and properties of base class without explicitly mentioning base class name.

Example:

In the example below, the __init__() function of base class polygon is overridden by __init__() function of derived class square. Hence, object of derived class square requires only one parameter to initialize the object. To access all the methods and properties of base class polygon, super() function is used in derived class square.

class polygon:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth
  def area(self):
    return self.length * self.breadth
  
class square(polygon):
   def __init__(self, side):
      super().__init__(side, side)
    
MySquare = square(3)
print(MySquare.area())

The output of the above code will be:

9

Adding properties and methods in derived class

A new property or method can be added to derived class in the same way it is added to base class.

Example:

In the example below, a new property description and new method perimeter is added to derived class square. This method and property is not available to object of base class polygon.

class polygon:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth
  def area(self):
    return self.length * self.breadth
  
class square(polygon):
  def __init__(self, side):
      self.side = side
      super().__init__(side, side)

  description = "This is a new property."
  
  def perimeter(self):
      return 4*self.side
    
MySquare = square(10)
print(MySquare.description)
print(MySquare.area())
print(MySquare.perimeter())

The output of the above code will be:

This is a new property.
100
40