Say I have this class hierarchy:
Animal <-- FourLegged <-- Dog
Animal <-- FourLegged <-- ....
Animal <-- ThreeLegged <-- ... (what has 3 legs?)
Animal <-- TwoLegged <-- Chicken
Animal <-- TwoLegged <-- ...
(Forgot what the scientific terms for four-legged and two-legged animals are. But that's beside point, right? :) )
If I have an Animal
object, and I need to know whether it is a FourLegged
or if it is a TwoLegged
, I would normally do animalObject instanceof FourLegged
.
Is that slower than adding a bunch of isFourLegged()
, isThreeLegged()
, isTwoLegged()
, to Animal
class?
(I would like to know the performance impact. This is NOT a design exercise. So I would appreciate it if you won't be wasting time suggesting how to re-write the class)
Edit Some people have suggested adding 'getNumberOfLegs()", which is perfectly reasonably given this specific example. But suppose the point is NOT to have the number of legs, but to get what type of animal this is.