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.

Class Syntax

The basic syntax for creating a class is ?

class NameOfClass:
    # Statement

Basic Class Example

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 ?

Class created successfully

Creating Subclasses with super()

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.

Syntax

The syntax is as follows ?

super()

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

Basic Inheritance Example

The following example demonstrates how to use super() to call parent class methods ?

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 ?

Animal Type: Reptile
Reptiles are cold blooded

Accessing Parent Attributes

The following example shows how subclasses can access parent class attributes ?

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)

# Creating an instance and accessing parent attribute
gaming_laptop = Games(5, 9)
print("Area:", gaming_laptop.area)

Following is the output ?

Area: 50

Single Inheritance Example

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 LackOfSweetnessTaste(self):
        if self.LackSweetTasteReceptor:
            print("It cannot detect sweetness.")

class Feline(Cat_Family):
    def __init__(self):
        super().__init__()

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 the output of the above code ?

It is digitigrade.
Has toes on forefeet and hind feet

Advantages and Limitations

The super() method in Python has two main advantages ?

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

The super function has the following limitations ?

  • The class and its methods that the super function references must exist.
  • The called function's arguments should match those of the parent method.
  • Proper method resolution order must be maintained in multiple inheritance.

Conclusion

Creating subclasses in Python using super() allows for clean inheritance and method overriding. Use super() to call parent class methods and maintain proper inheritance hierarchies in your object-oriented programs.

Updated on: 2026-03-24T19:42:05+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements