Interface in java



  • Using the keyword interface, you can fully abstract a class interface form its implementation.
  • That is, using interface, you can specify what a class must do, but not how it does it.
  • Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.
  • In practice, this means that you can define interfaces that don't make assumptions about how they are implemented.
  • Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces.
  • Syntax of interface:
         //Interface  syntax.
         Interface myInterface
         {
                int  a=10;
                public int getBalance();
                public void setBalance(int a);
         }

  • To implemented an interface, a class must create the complete set of methods defined by the interface.However, each class is free to determine the details of its own implementation.
  • By providing the interface keyword, Java allows you to fully utilize the "one interface,multiple methods" aspects of polymorphism.
  • Interfaces are designed to support dynamic method resolution at runtime.
  • Normally, in order for a method to be called form one class to another, both classes need to be present at compile time so the java compiler can check to ensure that the method signatures are compatible.
  • All the variables defined in the interface are default final and cannot be changed in subclass.