Inheritance


#1

Hi Everyone,

I have a doubt.
In the highlighted region, why is it required to initialize model,color and mpg again in Electric Car?

Anyways it is derived from Parent class.


#2

yes, but our ElectricCar has a battery, so we need to overwrite init of parent class because init of Car can't handle battery

its problematic, i know. I don't think its a very logic design for a program.

You could super() to make init of ElectricCar shorter

I think i would personally would go for named argument in the init method of Car


#3

I still have some problems with it:

class ElectricCar(Car):
def super(ElectricCar, self).init(self, battery_type):
battery_type = self.battery_type


#4

Did you even look at the topic you hijacked? We need to initialize all the instance variables (model, color, mpg and battery_type) again because we overwrite parent init method


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.