9. Inheritance - Any simpler codes?


#1



Hi, I am showing part of my code for the ElectricCar class. In the end, I simply redefine my arguments: model, color and mpg again in the ElectricCar class. But isn't ElectricCar already a DerivedClass from my BaseClass?

But again, when I defined my variable my_car, I realised it has to take all my arguments: model, color, mpg, battery_type.

If I defined it as:
class ElectricCar(Car):
----def init(self, battery_type):
---------self.battery_type = battery_type

My variable cannot call the class ElectricCar properly (as ElectricCar takes different number of arguments?)
my_car = ElectricCar("Honda", "blue", 80, "molten salt")

My other alternative tries:
(1) Using the super function does not make sense as I am not replacing some previous definition in my BaseClass
(2) Calling Car.init(self, model, color, mpg) did not work as well

So this is what I did:

 
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


Please explain to me why? Are there any simpler methods to resolve this?
It seems that the Derivedclass can only inherit the definitons but not my BaseInstance from the BaseClass.
Thanks in advance


#2

@tylerhuang: Inheritance sure is a complicated topic in any sort of programming languages, that implement such feature.

In fact ElectricCar is derived of Car but ElectricCar has one or more arguments than Car, if I'm not wrong.

Inheritance is meant to allow derived classes to inherit methods from their base class.
Think of it as a way to avoid you, the programmer, to write the exact same piece of code over and over again.

Of course, in this example it doesn't seem so useful but how about we think of more complex stuff where rewriting some piece of code takes time and time is money, trust me.

Here's another explanation about it:

So you've tried to set the values by calling an instance of Car to later set the values of another instance of ElectricCar? That might not actually work because we are talking about two different classes with two different objects.

Unless you use your object from the class Car to pass attribute by attribute to your object from the class ElectricCar's init() function where you call it, which would still require more arguments.

Also, your derived class will inherit their base's methods, not attributes so you'd still need to pass each of the given parameters to your function so things can work properly with your derived class.

 
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

The above code is correct but it kills the idea of inheritance.
If there are more simple ways to do it, this seems to be the simplest one but the one who uses more lines of code.

I hope I was clear because inheritance sure is a complicated topic that deserves some special attention so let me know if there's still something unclear about it.


#3

You should call the super() method in the ElectricCar baseclass:


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

Then have something like this when you initialize an instance of that class::


my_car = ElectricCar('molten salt', 'Lamborgini', 'black', 2004)