OK so I gather that Interfaces are a way to enforce that an object implements a certain amount of functionality, without having to use inheritance. Kind of like a contract.
One major reason is that you can create an object using an interface reference, similar to an abstract method. When you do this, every object which implements the interface can be assigned to it. For example, if Dog and Car both implement Washable, then you can do:
Washable wD=new Dog();
Washable wC=new Car();
If Washable has the public abstract method wash(), then you can do this:
wD.wash();
wC.wash();
and their respective methods will be called. This also means that you can accept an interface as a parameter for a method meaning you don't have to add unecessary code to deal with every class which implements a certain interface.
See here for a more detailed explanation: http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html
Generalization
Using JAVA Interface we can achieve generalization across sub-class. The generalization means here sub-classes having same behavior implemented in a different way.
Standardization
Interface allows to set standardization for all the sub-classes which implements it. It specifies "what" the sub-classes must have but doesn't enforce how it should have.
100 % Abstraction
Interface body provides 100% abstraction, so that the sub-class should not miss any implementation of abstract method. This isn't possible if we use abstract classes.
De-Coupling(Loose Coupling)
While developing an application, the code which interacts with end users can be loosely coupled to the code running on the server[B L C] by using interfaces.
Multiple Inheritance
Using interfaces we can achieve MI which is not possible using classes.
Right, you need to implement it ever time but you can implement it differently every time and any class that calls it doesn't need to worry about how it's implemented.
For example, if you have a Zoo object with a bunch of animals (new Tiger(), Lion(), Bear()) then your zoo can do for each Animal a in some collection a.eat() and it will work. The zoo doesn't care that there are three different types of animals that eat in totally different ways.
Interfaces are the only way to create multiple inheritance in Java.
Say you create a class Animal
. And all animals, including humans extend that. And each of those animals inherits common methods like eat, breathe, etc.
But now let's say you have a MathProblem
class.
And you want to have certain classes that can solve that problem by passing the problem to a solve(MathProblem problem)
method.
And you know that a Human
, but also a Computer
might solve the math problem. So they both need to be able to solve that problem. You might be able to get the Computer to extend some MathSolver
class that has the method, but Human already extends Animal, and can't extends anything else. So a better way is to make MathSolver an interface and have both Human
, Computer
, and any other classes that need to solve problems implement that.
Also note that a Human
and a Computer
might solve the problems in completely different ways, since their such different objects. That's what interfaces are best for. Defining certain abilities that cut across multiple inheritance hierarchies, and can have very different implementations, but can all be passed to a method that accepts any of them. Think of the Comparable
interface; it's not something a certain class of objects has, all sort of things can be compared, and usually in very different ways. But you can always call sort on a List
of Comparable
objects since you know they have a certain order, no matter if they're Numbers
, Animals
, Computers
or anything else (as long as they implement Comparable
and define their ordering).
If you are creating a number of classes all implementing such features and the implementation is only slightly different, this is going to be a lot of hard work.
In that case you are easily allowed to create another layer in you hierarchy of classes which implements Animal
but is an ancestor class for all animals that eat in some way, for example
class Herbivore implements Animal {
public void eat(Object food) {
...
}
}
class Cow extends Herbivore..
class Horse extends Herbivore..
and you are allowed to override eat
by using super.eat()
and changing only the slight part..
You should look forward code reuse and encapsulation of components at the same time.. then if your interface really doesn't characterize the class itself but just a component of it you can go by composition as suggested by Carl Manaster.
You are confusing interfaces and inheritance. They are different concepts and can complement to each other. If all the eat methods are only slightly different, then you can create a base class which will contain the common code and will be invoked from the subclasses through overriden methods which add the different parts. The base class can still implement the interface. Hope it is clear.