Reputation: 3715
I have a doubt when is it apt to use an Interface and when to use inheritance in OO design. Lets say I have a human class and it inherits from the Mammal class. The Mammal class will have instance variables like eyes, nose, ears and limbs. My question is whether the eat method should go inside the Mammal class or it should be in an Interface. I thought the eat, sleep, breathe method should go inside an Interface. Please let me know if my OO design is correct and the reasoning if my understanding is wrong.
Upvotes: 0
Views: 37
Reputation: 218798
My question is whether the eat method should go inside the Mammal class or it should be in an Interface.
Why can't it be both in an interface and in the base class?
(Note that I'm not 100% an expert on the mechanics of Java and don't know for certain how it would be implemented in code. This is a language-agnostic answer about object oriented design.)
An interface and a base class aren't interchangeable things, as the wording of your question seems to almost imply. They serve two very different purposes.
An interface is a kind of contract of functionality. It's a set of operations which can be implemented by an object. A base class, on the other hand, is a concrete type of what that object is. To think of it in a vague pseudo-analogy...
An interface tells you what something can do. A base type tells you what something is.
Consider your example... Mammal and Human. Now, clearly, Mammal must be an abstract type. You can't have a generic "mammal" knocking about in the world, it has to be a species of some kind. And consider that all mammals eat in some way.
Do all mammals eat the exact same way? If so, then you can create a single implementation of eat()
on the abstract base class and all inheriting types would use it.
Do most mammals eat the exact same way, but there are some exceptions? If so, then you can still create that base implementation of eat()
but allow inheriting types to override it in their own implementations if they choose to do so.
Do most or all mammals eat in entirely different ways, but all eat somehow? If so, then you'd likely want to create an abstract eat()
method signature in the base class and require that inheriting types implement it.
Ok, so where do interfaces come in?
Well, mammals aren't the only things that eat. Birds eat. Lizards eat. Plants eat. Robots eat. Fire eats. etc.
Lots of things eat. Not just mammals and their descendant types. Not even just their ancestor types. Not even just types that are remotely related to mammals in any way. (Fire, for example.) So the concept of "eating" is orthogonal to the type hierarchy in your inheritance for mammals.
In order to group together these implementations which aren't related via an inheritance model (or at least aren't guaranteed to be), you'd define an interface.
Suppose you have an operation which simply wants to feed an object. While other parts of the system may care whether or not that object is a mammal, or an animal of any kind, or even just a living thing of any kind... this operation does not. It just cares that the object can, in some way, eat. A robot or a fire or some other non-living thing could satisfy this requirement. This operation would declare that it expects an Eater
, not a Mammal
.
So somewhere in your abstract hierarchy of base types (living thing, animal, mammal, etc.) one of those types (probably the top-level one in this example) declares that it implements an Eater
interface. This would enforce that all inheriting types must also implement that interface.
If there's an abstract higher-level implementation which is inherited by the descendant type, then the interface is satisfied. If there isn't, then the descendant type would need to provide the implementation of eat()
to satisfy the interface.
But the point is that other things, entirely outside of this particular inheritance graph, can also implement that interface and can also eat.
Upvotes: 3
Reputation: 3785
You are correct. You can create an interface like Animal
which will contains those methods. In the subclasses (which implements Animal
) those methods should be implemented. Because e.g Mammal
, Reptile
have their different walking technique
Upvotes: 0