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

4th Apr 2020, 4:59 AM
Wasi
Wasi - avatar
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...)
4th Apr 2020, 5:57 AM
Tibor Santa
Tibor Santa - avatar
+ 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.
4th Apr 2020, 6:00 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks
4th Apr 2020, 6:05 AM
Wasi
Wasi - avatar
0
What if i did not created any vehicle class
4th Apr 2020, 5:58 AM
Wasi
Wasi - avatar