0
0
Pythonprogramming~5 mins

Super function usage in Python

Choose your learning style9 modes available
Introduction

The super() function helps you use methods from a parent class inside a child class easily. It avoids repeating code and keeps things organized.

When you want to add extra features to a method from a parent class without rewriting it.
When you have a child class that needs to build on the behavior of its parent class.
When you want to keep your code clean by reusing existing methods from a parent class.
When you are working with multiple classes that share some common behavior.
When you want to make sure the parent class method runs before or after the child class method.
Syntax
Python
super().method_name(arguments)

super() calls the parent class method with the same name.

You usually use it inside a method of a child class.

Examples
The child class calls the parent's greet method first, then adds its own message.
Python
class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    def greet(self):
        super().greet()
        print("Hello from Child")
The Dog class uses super() to call Animal's sound method, then adds "Bark".
Python
class Animal:
    def sound(self):
        print("Some sound")

class Dog(Animal):
    def sound(self):
        super().sound()
        print("Bark")
Sample Program

This program shows how the Car class uses super() to run the start method from Vehicle, then adds its own message.

Python
class Vehicle:
    def start(self):
        print("Vehicle started")

class Car(Vehicle):
    def start(self):
        super().start()
        print("Car is ready to go")

my_car = Car()
my_car.start()
OutputSuccess
Important Notes

If you forget to use super(), the parent method won't run automatically.

Using super() helps when you change the parent class later; child classes still work correctly.

Summary

super() lets child classes use parent class methods easily.

It helps avoid repeating code and keeps your program organized.

Use it inside child class methods to add or extend behavior from the parent.