9/11 Inheritance


#1

Hey there
Here is my code (it works):

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

`class ElectricCar(Car):`
    def __init__(self, model, color, mpg, battery_type):
        self.model = model
        self.color = color
        self.mpg   = mpg
        self.battery_type = battery_type

Now, both classes Car, and Electric cars include self, model, color and mpg.
Since they do, what is the use of inheritance when Electric car has model, color and mpg as well?
If I need class Tractor, I am going all over again to need model, color and mpg, so, WHAT IS THE USE OF CAR?


9/11 Inheritance
#2

I don't know if I understood your question correctly. Is the problem because you have to type the __init__() method with its variables and values again?
Do you remember override? You had to type the __init__() method in the ElectricCar class again because for that class you now included a fifth argument (battery_type); otherwise, the ElectricCar class would just inherit the __init__() method with its variables and values from the Car class and you wouldn't have to type them again.

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 %s MPG." %(self.color, self.model, self.mpg)

    def drive_car(self):
        self.condition = "used"

class ElectricCar(Car):

my_car = ElectricCar("DeLorean", "silver", 88)

print my_car.display_car()

There are two ways to inherit or obtain the variables and values from the __init__() method in the Car class and avoid having to type them again:

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        super(ElectricCar, self).__init__(model, color, mpg)
        self.battery_type = battery_type

And

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type

#3

I think I understand now. Thank you.
The question was:
If class1 has variables a and b
and class2 inherits a and b (and has c as well), why do I have to initialize a and b again.

What I am expecting to be (and that does not work) looks like this:

class1 has variables a and b
initiating a and b

class2 inherits a and b (which are already initialized so you don't have to do it again) and c
initiating c (because a and b are already initiated in class1)
Does this make any sense?


#4

I was confused about the same thing. The answer by rudy86 above was helpful in pointing out that this is an example of overriding methods (which is the subject of the next exercise in the lesson). Unfortunately, it is a particularly bad example of inheritance (which is what this exercise is supposedly about).


#5

Once the __init__() method is overridden in Class2 (because a new argument is given to Class2), a and b are no longer initialized in Class1 and the super() built-in function (or Class1.__init__(self, a, b)) is needed in order for a and b to be initialized in Class1.

class Class1(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b

class Class2(Class1):
    def __init__(self, a, b, c):
        super(Class2, self).__init__(a, b)
        self.c = c