What is super methon in python How to Call Method of a Particular Super Class...
- Get link
- X
- Other Apps
super() Method:
super() is a built-in method that is useful to call the superclass constructors, variables, and methods from the child class.
Demo Program-1 for super():
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def display(self):
print('Name:',self.name)
print('Age:',self.age)
class Student(Person):
def __init__(self,name,age,rollno,marks):
super().__init__(name,age)
self.rollno=rollno
self.marks=marks
def display(self):
super().display()
print('Roll No:',self.rollno)
print('Marks:',self.marks)
s1=Student('Durga',22,101,90)
s1.display()
Output:
Name: Durga
Age: 22
Roll No: 101
Marks: 90
In the above program, we are using the super() method to call the parent class constructor and
display() method
#Demo Program-2 for super():
class P:
a=10
def __init__(self):
self.b=10
def m1(self):
print('Parent instance method')
@classmethod
def m2(cls):
print('Parent class method')
@staticmethod
def m3():
print('Parent static method')
class C(P):
a=888
def __init__(self):
self.b=999
super().__init__()
print(super().a)
super().m1()
super().m2()
super().m3()
c=C()
Output:
10
Parent instance method
Parent class method
Parent static method
In the above example, we are using super() to call various members of the Parent class.
- Get link
- X
- Other Apps
Comments
Post a Comment