Super Keyword in Python

 Super Keyword in python:

  • The super() keyword in Python is used to call methods or constructors from a parent class. It helps access inherited methods, enabling method overriding and avoiding direct class name references.


Program:


# Class A: Prints "A" and defines a display method

class A():
    def __init__(self):
        print("A")  # Constructor prints "A"
    
    def display(self):
        print("This is cabbin A")  # Display method for A

# Class B: Calls A's constructor and prints "B"
class B():
    def __init__(self):
        super().__init__()  # Calls A's constructor
        print("B")  # Constructor prints "B"
    
    def display(self):
        print("This is cabbin B")  # Display method for B

# Class C: Inherits from A and B, calls their constructors and prints "C"
class C(A,B):
    def __init__(self):
        super().__init__()  # Calls A and B's constructors
        print("C")  # Constructor prints "C"
    
    def display(self):
        print("This is cabbin C")  # Display method for C

# Creating an object of C
obj = C()  # Calls constructors of A, B, and C



Output:

A
B
C

Post a Comment

0 Comments