Write a program to access the base class constructor from a sub class by using super()
method and also without using super() method.
class base:
def __init__(self,v):
self.value=v
def display_val(self):
print("Value is", self.value)
class derived(base):
def __init__(self,v,v1):
self.value1=v1
super().__init__(v)
def display_val1(self):
print("Value1 is",self.value1)
d1=derived(5,10)
d1.display_val()
d1.display_val1()
b1=base(20)#without super method
b1.display_val()
'Write a program to override super class constructor and method in sub class.
class superclass:
def __init__ (self,x):
self.x=x
def printx(self):
print("X value is", self.x)
class subclass(superclass):
def ___init__(self,x):
self.x=x
def printx(self):
print("X value is",self.x)
super= superclass(5)
super.printx()
sub=subclass(10)
sub.printx()
Write a program to implement single inheritance in which two sub classes are derived from a single base class.
class base:
b1=100;
@classmethod
def printb1val(cls):
print("b1 value is",cls.b1)
class der1(base):
d1=50
@classmethod
def printd1val(cls):
print("d1 value is",cls.d1)
def printb1_d1val(cls):
print("b1 and d1 value is",(base.b1+der1.d1))
#print("d1 and b1 value is",(base.b1+cls.d1))
class der2(base):
d2=25
@classmethod
def printd2val(cls):
print("d2 value is",cls.d2)
def printb1_d2val(cls):
print("b1 and d2 value is",(base.b1+der2.d2))
#print("d1 and b1 value is",(base.b1+cls.d2))
d1=der1()
d1.printb1val()
d1.printd1val()
d1.printb1_d1val()
d2=der2()
d2.printb1val()
d2.printd2val()
d2.printb1_d2val()
Write a program to understand the order of execution of methods in several base
classes according to method resolution order (MRO).program of inheritance problems in multiple inheritance Mehtod resolution order(MRO)
class A:
def printdata(self):
self.a=10
print(self.a)
super().printdata()
class B:
def printdata(self):
self.b=20
print(self.b)
class C(A,B):
def printdata(self):
self.c=30
super().printdata()
print(self.c)
c1=C()
c1.printdata()
method and also without using super() method.
class base:
def __init__(self,v):
self.value=v
def display_val(self):
print("Value is", self.value)
class derived(base):
def __init__(self,v,v1):
self.value1=v1
super().__init__(v)
def display_val1(self):
print("Value1 is",self.value1)
d1=derived(5,10)
d1.display_val()
d1.display_val1()
b1=base(20)#without super method
b1.display_val()
'Write a program to override super class constructor and method in sub class.
class superclass:
def __init__ (self,x):
self.x=x
def printx(self):
print("X value is", self.x)
class subclass(superclass):
def ___init__(self,x):
self.x=x
def printx(self):
print("X value is",self.x)
super= superclass(5)
super.printx()
sub=subclass(10)
sub.printx()
Write a program to implement single inheritance in which two sub classes are derived from a single base class.
class base:
b1=100;
@classmethod
def printb1val(cls):
print("b1 value is",cls.b1)
class der1(base):
d1=50
@classmethod
def printd1val(cls):
print("d1 value is",cls.d1)
def printb1_d1val(cls):
print("b1 and d1 value is",(base.b1+der1.d1))
#print("d1 and b1 value is",(base.b1+cls.d1))
class der2(base):
d2=25
@classmethod
def printd2val(cls):
print("d2 value is",cls.d2)
def printb1_d2val(cls):
print("b1 and d2 value is",(base.b1+der2.d2))
#print("d1 and b1 value is",(base.b1+cls.d2))
d1=der1()
d1.printb1val()
d1.printd1val()
d1.printb1_d1val()
d2=der2()
d2.printb1val()
d2.printd2val()
d2.printb1_d2val()
Write a program to understand the order of execution of methods in several base
classes according to method resolution order (MRO).program of inheritance problems in multiple inheritance Mehtod resolution order(MRO)
class A:
def printdata(self):
self.a=10
print(self.a)
super().printdata()
class B:
def printdata(self):
self.b=20
print(self.b)
class C(A,B):
def printdata(self):
self.c=30
super().printdata()
print(self.c)
c1=C()
c1.printdata()