The time has come to understand your thought.
If you want to do something like this, you need to drop Vehicle on the Bus . However, usually you should have an interface common to all cars.
Since issueTicket() does not apply to bicycles, you could probably consider not using the same interface for buses and bicycles in general.
Another idea is to implement the issueTicket() method for bead, which simply signals an error as soon as it calls:
Vehicle[] myFleet = {bus1, bike1, bus2}; // Note that Vehicle is an abstract class for (Vehicle v: myFleet){ v.issueTicket(1.50); System.out.println(v); }
However, in my opinion, it still looks like a design that could be better.
So that I can offer a few more suggestions, it would be nice to know the reason why these objects should be stored in one container.
Hope this helps.
Regards, JΓΆrg
source share