How do I make a subclass from a super class in Python?

In this article we are going to discuss how to create subclass from a super class in Python. Before proceeding further let us understand what is a class and a super class.

A class is a user-defined template or prototype from which objects are made. Classes offer a way to bundle together functionality and data. The ability to create new instances of an object type is made possible by the production of a new class.

Each instance of a class may have attributes connected to it to preserve its state. Class instances may also contain methods for changing their state that are defined by their class.


Syntax used for class is −

class NameOfClass:
# Statement


The class keyword denotes the creation of a class, followed by the class name i.e. ‘Sports’ in the following example −

class Sports: pass print ('Class created successfully')


The output of the above code is as follows −

Class created successfully

Creating a super class object in subclass in Python

Access to the methods and properties of a parent or sibling class is provided by the super() function. In addition to allowing for multiple inheritances, the super() function returns an object that represents the parent class.


The syntax is as follows −


It returns a proxy object that reflects the parent's class and has no parameter.


The example of super() function is as follows −

class Mammal(object): def __init__(self, Mammal_type): print('Animal Type:', Mammal_type) class Reptile(Mammal): def __init__(self): # calling the superclass super().__init__('Reptile') print('Reptiles are cold blooded') snake = Reptile()


Output of the above code is as follows −

Animal Type: Reptile
Reptiles are cold blooded


The following example explains the use of super() function in python −

class Laptop(object): def __init__(self, breadth, height): self.breadth = breadth self.height = height self.area = 50 class Games(Laptop): def __init__(self, breadth, height): super(Games, self).__init__(breadth, height)


Following is the output of the above code where we can access Laptop.area −

# Picking up 5 and 9 for breadth and height respectively
>>> x=Games(5,9)
>>> x.area


Single inheritance using super()

Take Cat_Family as an example. Cat_Family includes Feline, Tigers, and Lynx. They also have traits in common like −

  • They are digitigrade.
  • They have five toes on their forefeet and four toes on their hindfeet.
  • They cannot detect the sweetness.

Feline, Tiger, and Lynx are therefore subclasses of the Cat Family class. Because numerous subclasses are inherited from a single parent class, this is an illustration of single inheritance.

class Cat_Family: # Initializing the constructor def __init__(self): self.digitigrade = True self.ToesOnForefeet = 5 self.ToesOnHindfeet = 4 self.LackSweetTasteReceptor = True def isDigitigrade(self): if self.digitigrade: print("It is digitigrade.") def LackOfSweetnessTste(self): if self.LackSweetTasteReceptor: print("It cannot detect sweetness.") class Feline(Cat_Family): def __init__(self): super().__init__() def isMammal(self): super().isMammal() class Tigers(Cat_Family): def __init__(self): super().__init__() def hasToesOnForefeetAndHindfeet(self): if self.ToesOnForefeet and self.ToesOnHindfeet == 4: print("Has toes on forefeet and hind feet") # Driver code Pet = Feline() Pet.isDigitigrade() Street = Tigers() Street.hasToesOnForefeetAndHindfeet()


Following is an output of the above code −

It is digitigrade.
Has toes on forefeet and hind feet

Applications and Restrictions of the Python super() method

The Super() method in Python has two main applications −

  • Allows us to avoid explicitly utilising the base class name.
  • Handling Multiple Inheritance

The super function has the following three restrictions −

  • The class and its methods that the super function references
  • The called function's arguments should match up with those of the super function.
  • After you use it, super() must be included in every instance of the method.