-1

I am not able to call the variables and constructor of parent class using inheritance.

class base():
    #age =24;
    def __init__(self,age):
        self.age = age;
        print("i am in base")

class Date(base):
    def __init__(self,year,month,date):
        self.year = year
        self.month = month
        self.date = date

class time(Date):
    def greet(self):
        print('i am in time')

a = time(1,2,4)
print(a.year)
print(a.age) #this line is throwing error...

Please help, how to call the constructor of the parent class

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

1

What you are doing in your example is not multiple inheritance. Multiple inheritance is when the same class inherits from more than one base class.

The problem you are having is because none of your child classes call the parent constructor, you need to do that and pass the required parameters:

class Date(base):
    def __init__(self, age, year, month, date):
        super().__init__(age)
        self.year = year
        self.month = month
        self.date = date

class time(Date):
    def __init__(self, age, year, month, date):
         super().__init__(age, year, month, date)

    def greet(self):
        print('i am in time')
Isma
  • 14,604
  • 5
  • 37
  • 51