What is an interface?
An interface is a set of method definition without implementation. It is a protocol of behavior for a class.
ExampleLet’s create an interface for a simple mathematical calculation application. In the interface, we will keep all required vocabulary so that we will not miss anything. Preparing an interface is a one time exercise. We can use same interface and implement it as required whenever we require similar functionality.
public interface ICalculate // here we have defined all necessary vocabularies.
{
void add(int a, int b);
void multiply(int a , int b) ;
void divide(int a , int b) ;
}
public class clsCalculator implements ICalculate // We have implemented above methods
{
void add(int a, int b)
{
return a+b;
}
void multiply(int a , int b)
{
return a*b;
}
void divide(int a , int b)
{
return a/b;
}
public void main()
{
add(10,5);
multiple(10,5);
divide(10,5);
}
}