Python Inheritance

Python Inheritance

Python inheritance is one of the object-oriented programming concepts. It allows a class to have methods and properties of another class. The class which is being inherited is called the parent class or the base class. The class which inherits properties and methods is called the child class or the derived class. With inheritance, code reusability is achieved and more functions can be added to the child class without modifying the parent class.

 

 

Create Derived class

In Python, to create a derived class, name of the base class is a must to be specified. class keyword is followed by the name of the derived class and name of the base class enclosed in paranthesis. Syntax of creating  a derived class in Python is as follows:

Python Inheritance/Create Derived Class Syntax:

#Parent class
class parent_class:
    statements

#Child class
class child_class(parent_class):
    statements

Python Inheritance Example:

# Python program to exemplify creation of derived class

class polygon:
  def __init__(self, len, br):
    self.len = len
    self.br = br

class rectangle(polygon):
  pass

Rectangle = rectangle(2, 5)
print(Rectangle.len)
print(Rectangle.br)

Output:

2
5

 

 

__init__() Function of Derived Class

__init__() function of the derived class is the constructor of that class. If the base class of the derived class also has the __init__() function, then it is overridden by the __init__() function of the derived class when an object of the derived class is created. If the derived class does not have the __init__() function, then it inherits it from the base class if it has.

__init__() Function of Derived Class Example:

# Python program to exemplify __init__() function of the derived class

class polygon:
  def __init__(self, len, br):
    self.len = len
    self.br = br
  def area(self):
    return self.len * self.br
  
class square(polygon):
   def __init__(self, side):
      polygon.__init__(self, side, side)
    
Square = square(7)
print(Square.area())

Output:

49

 

 

The super() Function

In Python, the super() function is used to initialize the base class object from the derived class. It calls the __init__() function of the base class and then use the arguments to initialize the properties of the base class object.

The super() Function Example:

class polygon:
  def __init__(self, len, br):
    self.len = len
    self.br = br
  def area(self):
    return self.len * self.br
  
class square(polygon):
   def __init__(self, side):
      super().__init__(side, side)
    
Square = square(7)
print(Square.area())

Output:

49

 

 

Adding Properties and Methods in Derived Class

New properties and methods are added in the derived class in the same manner as they are added in the base class.

Adding Properties and Methods in Derived Class Example:

# Python program to exemplify addition of new properties and methods in the derived class

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

  new_property = "This is a new property."
  
  def perimeter(self):
      return 4*self.side
    
Square = square(20)
print(Square.new_property)
print(Square.area())
print(Square.perimeter())

Output:

This is a new property.
400
80

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *