Java Concepts‎ > ‎


An interface is an entity through which unrelated objects interact with each other. An interface could also resemble the rules of a communication protocol or an adherence of two sides to a business agreement (one side agrees to do a service while the other side promises to pay money). Similarly, a class that implements an interface agrees to implement all of the methods defined in the interface (agrees to a certain behavior).

However, a Java interface can declare methods but cannot implement them, because method bodies are missing:
// Declare an interface -- methods declared in the interface are public and abstract
public interface Capable {
    public void displayGreeting();  // the method header is followed by a semicolon
Note: An interface is not an object; therefore, it cannot be instantiated.
Note: However, interfaces can be extended by other interfaces or implemented by classes.

To implement an interface class must use implements keyword:
// A class can extend only one superclass, but it can implement multiple interfaces
public class Greeting implements Capable {
    public void displayGreeting(){
        System.out.println("Hi! Are you fresh in interfaces???");
An object can be referenced by multiple data types: by the type of every interface they implement as well as by the type of their class. Therefore, a variable declared as an interface type can reference any object that is instantiated from any class implementing the interface.

public static void main(String args[]){
    Greeting hello = new Greeting();
Linas Vaicekavicius,
Oct 12, 2010, 4:14 AM