What's the point of Inheritance?


#1



9. Inheritance (Car/ElectricCar classes)


I figured out how to make the code work, but in doing so it made me not understand the point of Inheritance - This will make more sense when you look at the code...


I have commented out the lines I added for which the program needed to work. I figured since I was using inheritance, I wouldn't need to use those lines of code. What is the point of Inheritance if you have to basically add in everything it inherits anyways, why not just make a whole separate method?


Replace this line with your code. 
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):
    
    def __init__(self, model, color, mpg, battery_type):
        self.battery_type = battery_type
       ''' self.model = model
        self.color = color
        self.mpg = mpg '''
        

my_car = Car("DeLorean", "silver", 88)
my_car = ElectricCar("Volvo", "black", 30, "molten salt")

print my_car.condition

my_car.drive_car()

print my_car.condition


#2

If you override a method then you're not inheriting it.

Inheritance lets you create classes whose objects are what the parent class describe, but with compatible changes. For example, a zebra is a horse with stripes. (I'm sure that's super correct)

When and if you need it you'll know, until then you don't really need to pay attention to it.

It's particularly useful when some kind of library/framework needs an object with particular behaviour, that library/framework may add everything that you don't need to write yourself and you add in the rest.


#3

This doesn't seem to answer the original poster's question, which I am confused by as well. Overriding the methods is the only way to get the code to work. I thought that listing Car as the base class for ElectricCar would mean that the model, color, and mpg are imported over, but they aren't. I've tried to access them through the super expression, but that doesn't seem to work either.


#4

If you override the method that sets those, then that method in the parent class won't run, that's what overriding does.


#5

I agree with the OP poster, this really doesn't seem to make sense.
I had the same problem, I don't understand why below doesn't work

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):
def init(self, battery_type):
self.battery_type=battery_type

my_car = Car("DeLorean", "silver", 88)
my_car=ElectricCar("molten salt")

print my_car.condition

my_car.drive_car()

print my_car.condition


#6

What I am getting from ionatan is that inside of ElectricCar() we redefine __init__ (it ask us to) we no longer inherit that because its overriding the _init__ its inherting from Car() . In other words if you leave out the init method in ElectricCar ( not what the exercise asks) you can then pass on the old information (model, color, mpg) when declaring a new ElectricCar variable. See below (NOTE this is not what the exercise is asking us to do tho)

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

        
my_car = ElectricCar('DeLorean', 'red', 88)
my_car.battery_type = "molten salt"

#7

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