9. Inheritance


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
Unless I’m missing something obvious (could well be the case), for the third line in the code below in the Inheritance exercise, the instructions do not state at any stage this syntax or its purpose, and unless one has worked with Python or other languages in the past, they won’t get it. Of course once a student gets stumped enough to open the forums the answer is pretty evident, but it would be great to have this included in the lesson. Instructions below for reference:

"Create a class ElectricCar that inherits from Car. Give your new class
 an __init__() method of that includes a "battery_type" member variable in
 addition to the model, color and mpg.

Then, create an electric car named "my_car" with a "molten salt" battery_type. 
Supply values of your choice for the other three inputs (model, color and mpg)."

And the hint:

Redefining a method of a "child" class is as simple as including
a definition for that function inside the "child" class; this version will 
take precedence over the inherited version.
Remember to include the self keyword as the first input when you define
the __init__() method!


<What do you expect to happen instead?>


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

<do not remove the three backticks above>

Agreed. The lesson needs to make this a bit more clear. In seeing the solution, I completely understand why it would be needed, but I did not arrive at that in the lesson itself - had to come here to find it.

1 Like

Me as well. I assumed that a new class that inherits from a parent class automatically inherits the init method of the parent class.

1 Like

I had the exact same reasoning.

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