• To declare a class that implements an interface, you include an implements clause in the class declaration.


  • Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.


  • By convention, the implements clause follows the extends clause, if there is one.


  • A Sample Interface, Relatable Consider an interface that defines how to compare the size of objects.


  •  
    public interface Compare {
        public int isLargerThan(int length, int length2);
    }
    
    
  • Implementing the Compare Interface Here is the Math class to implement Relatable.


  •  
    public class Math 
        implements Relatable {
       
        public int isLargerThan(int length, int length2) {
           
           
            if (length < length1)
                return length;
           
            else
                return length1;               
        }
    }
    
    
  • Because Math implements Relatable, it has to implement method isLargerThan(int, int)


  • When you define a new interface, you are defining a new reference data type.


  • You can use interface names anywhere you can use any other data type name.


  • In below program, we have created an interface reference named obj1. We have also created object t1 of class testClass1. We now assign to reference obj1 an instance of a class testClass1 that implements the interface.


  •  
    
    
    
    interface interface1 
    { 
    	void display(); 
    } 
    
    
    class testClass1 implements interface1 
    { 
    
    	public void display() 
    	{ 
    		System.out.println("Test class 1"); 
    	} 
    
    } 
    
    
    class Main
    { 
    
    	public static void main (String[] args) 
    	{ 
    		testClass1  t1 = new testClass1(); 
    		interface1 obj1;   //create a reference
    		obj1=(interface1)t1;   // assign object to it
    		obj1.display();
    	
    	
    	} 
    } 
    
    
  • By casting t1 to the reference of interface type, it can invoke the display method.


  • Consider an interface that you have developed called DoIt:


  •  
    public interface DoIt {
       void doSomething(int i, double x);
       int doSomethingElse(String s);
    }
    
    
    
  • Suppose that, at a later time, you want to add a third method to DoIt, so that the interface now becomes:


  •  
    public interface DoIt {
    
       void doSomething(int i, double x);
       int doSomethingElse(String s);
       boolean didItWork(int i, double x, String s);
       
    }
    
    
    
  • If you make this change, then all classes that implement the old DoIt interface will break because they no longer implement the old interface.


  • Programmers relying on this interface will protest loudly.


  • Try to anticipate all uses for your interface and specify it completely from the beginning.


  • If you want to add additional methods to an interface, you have several options.


  • You could create a DoItPlus interface that extends DoIt:


  •  
    public interface DoItPlus extends DoIt {
    
       boolean didItWork(int i, double x, String s);
       
    }
    
    
    
  • Now users of your code can choose to continue to use the old interface or to upgrade to the new interface.


  • Alternatively, you can define your new methods as default methods.


  • The following example defines a default method named didItWork:


  •  
    public interface DoIt {
    
       void doSomething(int i, double x);
       int doSomethingElse(String s);
       default boolean didItWork(int i, double x, String s) {
           // Method body 
       }
       
    }
    
    
    
  • Note that you must provide an implementation for default methods.


  • You could also define new static methods to existing interfaces.


  • Users who have classes that implement interfaces enhanced with new default or static methods do not have to modify or recompile them to accommodate the additional methods.


  • Default methods enable you to add new functionality to the interfaces of your libraries and ensure compatibility with code written for older versions of those interfaces.


  • Consider the following interface, interface1:


  •  
    
    interface interface1 
    { 
    	void display(); 
    } 
    
    
    class testClass implements interface1 
    { 
    
    	public void display() 
    	{ 
    		System.out.println("Test class 1"); 
    	} 
    
    } 
    
    
    class Main
    { 
    
    	public static void main (String[] args) 
    	{ 
    		testClass  t = new testClass(); 
    		t.display();
    	
    	
    	} 
    } 
    
    
  • Suppose that you want to add new functionality to the interface1 interface as below:


  •  
    
    
    interface interface1 
    { 
    	void display(); 
    	void secondDisplay();
    } 
    
    
  • Following this modification to the interface, you would also have to modify the class testClass and implement the method secondDisplay().


  • However, rather than leaving secondDisplay as abstract , you can instead define a default implementation. (Remember that an abstract method is a method declared without an implementation.)


  •  
    
    interface interface1 
    { 
    	void display(); 
    	default void secondDisplay()
    	{
    	System.out.println("Second display");
    	}
    } 
    
    
  • You specify that a method definition in an interface is a default method with the default keyword at the beginning of the method signature.


  • All method declarations in an interface, including default methods, are implicitly public, so you can omit the public modifier.


  • With this interface, you do not have to modify the class testClass, and this class (and any class that implements the interface interface1), will have the method secondDisplay() already defined.


  • The following example shows the entire working:


  •  
    
    interface interface1 
    { 
    	void display();
    	default void secondDisplay()
    	{
    	    System.out.println("Second display");
    	}
    } 
    
    
    class testClass implements interface 
    { 
    
    	public void display() 
    	{ 
    		System.out.println("Test class 1"); 
    	} 
    
    } 
    
    
    class Main
    { 
    
    	public static void main (String[] args) 
    	{ 
    		testClass  t = new testClass(); 
    		t.display();
    		t.secondDisplay();
    	
    	
    	} 
    } 
    
    
    Output:
    Test class 1                                                                                    
    Second display
    
    
    
  • When you extend an interface that contains a default method, you can do the following:


    1. Not mention the default method at all, which lets your extended interface inherit the default method.


    2. Redeclare the default method, which makes it abstract.


    3. Redefine the default method, which overrides it.


  • Suppose that you extend the interface interface1 as follows:


  •  
    
    public interface interface2 extends interface1 { }
    
    
    
  • Any class that implements the interface interface2 will have the implementation specified by the default method secondDisplay().


  • Suppose that you extend the interface interface1 as follows:


  •  
    
    public interface interface2 extends interface1 {
        void secondDisplay();
    }
    
    
  • Any class that implements the interface interface2 will have to implement the method secondDisplay; this method is an abstract method like all other nondefault (and nonstatic) methods in an interface.


  • Suppose that you extend the interface interface1 as follows:


  •  
    
    public interface interface2 extends interface1 {
        default public void secondDisplay() {
            
            System.out.println("New second display");
        }
    }
    
    
  • Any class that implements the interface interface2 will use the implementation of secondDisplay() specified by this interface2 instead of the one specified by the interface interface1.


  • In addition to default methods, you can define static methods in interfaces.


  • (A static method is a method that is associated with the class in which it is defined rather than with any object.


  • Every instance of the class shares its static methods.)


  • This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class.


  • The following example defines a static method in interface interface1. There is no need to implement this static method in class testClass


  •  
    
    interface interface1 
    { 
    	void display();
    	static void secondDisplay()
    	{
    	    System.out.println("Second display");
    	}
    } 
    
    
    class testClass implements interface1
    { 
    
    	public void display() 
    	{ 
    		System.out.println("Test class 1"); 
    	} 
    
    } 
    
    
    class Main
    { 
    
    	public static void main (String[] args) 
    	{ 
    		testClass t1 = new testClass(); 
    		t1.display();
    		interface1.secondDisplay();
    	
    	
    	} 
    } 
    
  • Like static methods in classes, you specify that a method definition in an interface is a static method with the static keyword at the beginning of the method signature.


  • All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.