Java Interface

Java Interface


Java Interface Notes

Java Interface

A Java interface declares methods that classes are expected to implement. It encapsulates a set of associated behaviors that can be used by dissimilar classes. Interfaces contain no instance fields and cannot be instantiated.

A class realizes (implements) an interface by promising to provide the implementation for all of the methods in the interface. An interface can be implemented by many classes and a class can implement multiple interfaces. All methods in an interface are automatically public and abstract.

public interface Flier
{
void fly( );
}

public class Bird implements Flier
{
   public void fly( ) // public must be written here
   {
     System.out.println("Using my wings to fly");
   }
}

public class Airplane implements Flier
{
   public void fly( )
   {
     System.out.println("Using my jet engines to fly ");
   }
}