Java interface - What is an interface?

What is an interface?

An interface is a set of method definition without implementation. It is a protocol of behavior for a class.

Example

Let’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);
   }
}
JAVA JVM and JIT - Explain JVM (Java virtual machine) and JIT (Just-in-time compilation)
JAVA JVM and JIT - JVM is a virtual computer that runs the compiled java program. JVM is software that stays on the top...
How to implement polymorphism in JAVA.
Java polymorphism - Capacity of a method to do different things based on the object that it is...
Java class loaders: What are class loaders?
Java class loaders - The class loader describes the behavior of converting a named class into the bits responsible for implementing that class...
Post your comment
Discussion Board
am i missing something here
you created an interface Icalculate with void return type and then you implemented these methods but you created return type...so what i'm asking is ,isnt that a mistake because as far as i know void doesnt return any value...i need an answer please
omri 02-17-2013