I can't understand ---> Inheritance



if i have to redefine all the arguments (color , model , mpg) to make the derived class work , then what
is the purpose of inheritance

i mean if replace class ElectricCar(Car) with class ElectricCar(object) it gives the same result
then why do i have to inherit from the Car Class ??

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self):
        return "This is a %s %s with %d MPG." % (self.color, self.model, self.mpg)
    def drive_car(self):
        self.condition = "used"

my_car = Car("DeLorean", "silver", 88)
print my_car.model
print my_car.color
print my_car.mpg
print my_car.condition
print my_car.condition

class ElectricCar(Car):
    def __init__(self,model, color , mpg ,battery_type):
        self.battery_type = battery_type
        self.model = model
        self.color = color
        self.mpg = mpg
my_car = ElectricCar("DeLorean", "silver", 88,"molten salt")


to inherit condition member variable, and display and drive car methods

once you learned super(), you can improve the init method of your electric car


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