AlphaCodingSkills

Python - Inheritance


Advertisements

Previous Page Next Page

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. Please see the syntax below:

Syntax


#Base class
class base_class:
    statements

#Derived class
class derived_class(base_class):
    statements

Example:


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)

Output

3
5

In above example, a base class called polygon and derived class called rectangle are created. rectangle inherits height and width property from its base class polygon.

__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:


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())

Output

9

In above example, 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.

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:


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())

Output

9

In above example, 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.

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:


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())

Output

This is a new property.
100
40

In above example, 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.






Previous Page Next Page