Does it also inherit if we don't create an init() in the child?


#1

Hey thanks for that bit of code. Question tho: so without that, a child class would only inherit the ability to use methods associated with the parent class and not what attributes can be passed?? Or does it also inherit if we don't create an init() in the child?


Inheritance
#2

Every ElectricCar instance is also a Car instance, so yes, even without the description attributes the instances still inherit the methods and class variables of the super class. ElectricCar has no description attributes unless they are written into the subclass directly, or initialized by the super (as in the above code example).

This doesn't answer my question about what lesson this is. As stated, we might be jumping the queue..


#3

Thanks, I think this is 'Classes 9. Inheritance' To clarify If I wrote ElectricCar() like this without an __init__

class ElectricCar(Car):
    battery_type= ""

my_car = ElectricCar('DeLorean', 'red', 88)

I could then pass on the same parameters when creating a new ElectricCar that I could when creating a Car(). But when we write in a init inside of ElectricCar it overrides the inherited __init__ and will have unique parameters that can be passed?


#4

It's the same code as textcoder27589. However I have been out for a few hours although just before I went out I had done some more reading and have just modified my code from the above and it now works fine thanks.


#5

What happens when you run the display_car() method on your instance?


#6

My apologies, one more thing the instructions ask us

Give your new class an init() method of that includes a "battery_type" member variable in addition to the model, color and mpg.

Are member variables are usable in the whole scope of a class? Such as we would be declaring that before our init and not passing it through?

This

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"

vs

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
        
my_car = ElectricCar('DeLorean', 'red', 88, "molten salt")

Thanks


#7

When using

class ElectricCar(Car):
    battery_type= ""

my_car = ElectricCar('DeLorean', 'red', 88)

It gives me all the relative information correctly. I should note I have a working solution(s) I am just trying to understand inheritance better.


#8

The next lesson discusses overriding inheritence. Review these lessons and play around a little with the code, see if the outcomes match your expectations. Be sure to reinforce this basic introduction with more reading on this topic. I've invited another member to this discussion for a more academic view.


#9

Awesome, will do and thank you for your time!


#10

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