In: Computer Science
Class Pie:
self__init__(self,a,b,c)
self.a = a
self.b = b
self.c = c
return
Class Food():
def _init__(self): #this __init__ is not important for the example
return
def getPie(): #no parameter
return (Pie) #Here is where I want to return the contents of the __init__ inside the Pie class.
So, I want to return the class Pie (the contents of __init__) inside of the class Food method getPie, which is a,b,c. How do I do that? This is Python.
class Pie:
def __init__(self,a,b,c):
self.a=a
self.b=b
self.c=c
class Food(Pie):
def __init__(self,a,b,c):
# invoking the constructor of
# the parent class
Pie.__init__(self,a,b,c)
def getPie(self):
print(self.a)
print(self.b)
print(self.c)
obj=Food(1,2,3)
obj.getPie()
When a class inherits from another class it inherits the attributes and methods of another class.
Here also class Food inherits from Pie and it is able to access attribute and methods of Pie.
To access parents class methods it can be done by simply call the constructor of Pie class inside the constructor of Food class and then Food class is able to access the methods and attributes of Pie class.