0
How can i do this?
I have a class car.It has a inner class Engine. Than i created a Ecar class inheriting from car class but ecars do not have Engine.They have a battery instead. So how can i discard the battery attribute?? Than i would add a battery class with it. https://code.sololearn.com/cX1pIk8K8AXm/?ref=app
4 Answers
+ 2
OK let's try to apply some logic here.
Car is a Vehicle.
Car has an Engine.
Ecar is also a Vehicle.
Ecar has no Engine, it has Battery.
So actually Ecar is not really a Car, it is a different kind of beast.
There could be multiple solutions to this, for example:
- Ecar extends Vehicle
- or Car should be an abstract class with children MotorCar (with combustion engine) and Ecar (with electric engine, because after all, electric cars also have engines, and fuel driven cars also have batteries...)
+ 1
Vehicle class was in the code you posted, that is why I mentioned.
The point is that according to your specification, Ecar cannot be a subclass of Car, but they can have a common ancestor.
+ 1
Thanks
0
What if i did not created any vehicle class



