jeudi 11 février 2016

How to categorize vehicles - inheritance vs. interfaces

I am trying to categorize vehicles but very confused that what should be the right way to categorize.

Why I am confused ? If you see vehicles can be categorized in number of ways.

1). Nature of Vehicle: Land Vehicle, WaterCrafts & AirCrafts. 2). Another way of categorizing: Powered Vehicles and NonPowered Vehicles like Tractor (Non powered and just attached with truck), Truck etc. 3). Loading nature of vehicle: Vehicle can be loaded or not.

Let me take an example: Vehicle -> Land Vehicle -> Powered/NonPowered -> Loadable/NonLoadable.

If you see, I am creating so many sub classes but whats use of it other than property difference that it is loadable or not. I can do that using interfaces also. Implements Loadable?powered interfaces to vehicle which can allow loading.

For example: vehicle -> LandVehicle -> Tractor LandVehicle will implement interfaces like isPowered and isLoadable, which will be overriden by tractor as false and true resp.

I know that Inheritance is for "isa" relationship and interfaces for "can do" but how that make difference here because it can be used interchangeably.

What is the best way to solve such kind of problems.

Aucun commentaire:

Enregistrer un commentaire