Search code examples
javaclassinterfacehierarchyabstract

Java: Need help constructing a class hierarchy which contains normal and abstract classes as well as interfaces


I must do this from the following list: - Bicycle - Car - Cycle - JetPlane - RoadVehicle - Refuelable - Train - Transport - Tricycle

I figured that transport would be at the top of the hierarchy as an abstract class. We could then have Cycle extend the transport class(cycle would then be extended by tricycle and bicycle). Refuelable could also extend the transport class, and have train, car and JetPlane extend the refuelable class. Lastly, RoadVehicle would be an interface and be extended by car.

I hope I have explained this problem clearly, my understanding of interfaces is a little cloudy.

Thanks in advance


Solution

  • I would do the following:

    interface Refulable...
    interface RoadVehicle...
    abstract class Transport...
    abstract class Cycle extends Transport...
    class Train extends Transport...
    class Car extends Transport implements Refulable, RoadVehicle...
    class JetPlane extends Transport implements Refulable...
    class Bicycle extends Cycle implements RoadVehicle...
    class Tricycle extends Cycle...
    

    Make Refulable an interface because it can be done by various classes across your hierarchy and an adjective also suggests an interface (see Comparable, Runnable...).

    I'd also make RoadVehicle an interface or you will get problems explaining what a Tricycle has to do on road.