There are always requirements like a particular class is to be able to perform certain actions,these actions will lead to get some special characteristics of that class. normally these actions are generic in nature and if in any case that action is so specific to that class then this action is not a candidate of interface.what generic action we can think of?? suppose we are designing classes for a program of a traffic monitoring system, we have classes like , vehicle,twowheeler,fourwheeler,car,bike,signal_system and there can be many more..for these vehicle related classes some generic actions we can think are shiftgear(),applybreak(),reverse(),forward() and so on. now there are many programmers , each writing their code and using other's too, they need to use code written by each others,one may write car class, another programmer may write bike class , someone is writing main class, so we are convinced with the fact that each will need to inter-operate with others (after all that's the basic architecture of an object oriented software ie,collection of objects, each using services provided by others and giving services to others).
now if these generic actions are known are to a programmer, and somehow he knows that a class can perform these type of actions, then he wont need to know much about that class, he will simply call the method and get the things done(we suppose that the programmer is intending to use the class only for the generic actions).
now these generic actions are specified in a thing which we call an interface, and if a class is providing these actions then that class will announce that by having a syntax like this.
class myimp implements someinterface
{
}
and by doing this, the class is supposed to provide the actions which have been specified in the implemented interface.
now by seeing only this thing one will know that yes!! this class provides actions specified in the interface which it's implementing.so i am being guaranteed to have that behavior given by the class.
hence a protocol is set among classes and hence progrmmers to use functions,actions,methods(all being talked with the same meaning in mind!!) provided by others.
in the example given above a suitable interface w'd be
interface movable
{
void applybreak();
void reverse();
void shiftgear();
void moveforward();
}we can see that these are very general kind of actions for vehicle related classes.
now the car class implements movable interface then it will provide implementation to those methods and hence some other person can easily call those methods.
class car implements movable
{
public void applybreak()
{
}
public void reverse()
{
}
public void shiftgear()
{
}
public void moveforward()
{
}
}
so in this way with the help of interfaces 1. a class can announce what things it can do and what's it like 2.a contract can be set among different programmers!!,so that they can easily and independently use classes written by each other for some generic type of behavior of that class.