If you want to access a, b, c using childtemp1 you need to pass a, b, c while creating the object
class A():
def __init__(self, a, b):
self.a = a
self.b = b
class B(A):
def __init__(self, a, b, c):
self.c = c
A.__init__(self, a, b)
class C(A):
def __init__(self, a, b, d):
self.d = d
A.__init__(self, a, b)
childtemp1 = B("Japan", "Germany", "California")
childtemp2 = C("Japan", "Germany", "Delhi")
print(childtemp1.a, childtemp1.b, childtemp1.c)
print(childtemp2.a, childtemp2.b, childtemp2.d)
Output:
Japan Germany California
Japan Germany Delhi
You can create child class using parent object
class A():
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return "a = " + str(self.a) + " b = " + str(self.b)
class B(A):
def __init__(self, parent, c):
self.c = c
A.__init__(self, parent.a, parent.b)
def __repr__(self):
return super().__repr__()+ " c = " + str(self.c)
class C(A):
def __init__(self, parent, d):
self.d = d
A.__init__(self, parent.a, parent.b)
def __repr__(self):
return super().__repr__()+ " d = " + str(self.d)
temp = A("Japan", "Germany")
childtemp1 = B(temp, 'India')
childtemp2 = C(temp, 'USA')
print(childtemp1)
print(childtemp2)
Output:
a = Japan b = Germany c = India
a = Japan b = Germany d = USA