I can't understand ---> Inheritance


#1


https://www.codecademy.com/courses/python-intermediate-en-egNXj/1/3?curriculum_id=4f89dab3d788890003000096


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
my_car.drive_car()
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")


#2

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


#3

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