super() method in Python

The super() method is used to give access to methods and properties of a parent (or superclass) from a child (or subclass).

It’s most commonly used in:
• Constructors (__init__ methods) to initialize the parent class.
• Overridden methods, when you want to extend or modify the parent’s behavior.

Basic Syntax super().method_name(arguments)
• super() → returns a temporary object that allows you to access the parent class methods.
• method_name → the parent class method you want to call.

Example: Basic Use in Inheritance.

Python

# Parent class
class Parent:
    def show(self):
        print("This is the Parent class")

# Child class inheriting Parent
class Child(Parent):
    def show(self):
        # Call parent class method using super()
        super().show()
        print("This is the Child class")

# Create object of Child
obj = Child()
obj.show() 

Output: This is the Parent class
This is the Child class

Explanation: • The child class overrides show().
• super().show() calls the parent version first.
• Then the child’s own code runs afterward.

Example: Using super() in Constructors (__init__).

Python

# Parent class
class Animal:
    def __init__(self, name):
        self.name = name
        print(f"Animal __init__ called, Name: {self.name}")
    
    def speak(self):
        print("Animal makes a sound")

# Child class
class Dog(Animal):
    def __init__(self, name, breed):
        # Using super() to call the parent constructor
        super().__init__(name)
        self.breed = breed
        print(f"Dog __init__ called, Breed: {self.breed}")

    def speak(self):
        # Extend the behavior of parent class
        super().speak()  
        print("Dog barks")

# Create object
dog1 = Dog("Tommy", "Labrador")
dog1.speak() 

Output: Animal __init__ called, Name: Tommy
Dog __init__ called, Breed: Labrador
Animal makes a sound
Dog barks

Here: • When the object is created Dog("Tommy", "Labrador") the child’s class constructor is called.
• super().__init__(name) calls the parent constructor.
• super().speak() calls the parent method before extending it.