10 Overriding methods


#1

how does it know that the condition is "new"?

print my_car.condition
my_car.drive_car()
print my_car.condition

my_car, is refered to ElectricCar, and there are no "condition" variables.....

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

class ElectricCar(Car):
    def __init__(self, color, model, mpg, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type
    def drive_car(self):
        self.condition = "like new"

my_car = ElectricCar("Ford", "Blue", 100, "molten salt")
print my_car.condition
my_car.drive_car()
print my_car.condition

#2

Hi @rubysurfer09367 ,

The following statement, inside the Car class definition, but outside any of its methods, assigned the value, "new", to condition, and established it as a member variable, or attribute, of the Car class ...

condition = "new"

This statement defined the ElectricCar class as inheriting from the Car class ...

class ElectricCar(Car):

So an ElectricCar is also a Car, and therefore, it has a condition attribute.

This statement instantiated my_car as an ElectricCar ...

my_car = ElectricCar("Ford", "Blue", 100, "molten salt")

So, my_car has a condition attribute that can be printed here ...

print my_car.condition