Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
An interface is similar to a class in the following ways:
- An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
- The byte code of an interface appears in a .class file.
However, an interface is different from a class in several ways, including:
- An interface does not contain any constructors.
- All of the methods in an interface are abstract.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
Declaring Interfaces
The interface keyword is used to declare an interface. Here is a simple example to declare an interface:
/* File name : NameOfInterface.java */ import java.lang.*; // Any number of import statements public interface NameOfInterface { // Any number of final, static fields // Any number of abstract method declarations\ }
Interfaces have the following properties:
- An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
- Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
Example:
/* File name : Animal.java */ interface Animal { public void run(); public void sleep(); }
Implementing Interfaces
A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
Example:
/* File name : MammalInt.java */ public class MammalInt implements Animal { public void eat() { System.out.println("Mammal run"); } public void travel() { System.out.println("Mammal sleep"); } public int noOfLegs() { return 0; } public static void main(String args[]) { MammalInt m = new MammalInt(); m.run(); m.sleep(); } }
Output
Mammal run Mammal sleep
Extending Interfaces
The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.
Example:
// Filename: Sports.java public interface Sports { public void setHomeTeam(String name); public void setVisitingTeam(String name); } // Filename: Wrestling.java public interface Wrestling extends Sports { public void homeTeamScored(int points); public void visitingTeamScored(int points); public void endOfQuarter(int quarter); } // Filename: Golf.java public interface Golf extends Sports { public void homeSwingScored(); public void visitingSwingScored(); public void endOfPeriod(int period); public void overtimePeriod(int ot); }
The Golf interface has four methods, but it inherits two from Sports; thus, a class that implements Hockey needs to implement all six methods. Similarly, a class that implements Wrestling needs to define the three methods from Wrestling and the two methods from Sports.
Extending Multiple Interfaces
Extending Multiple Interfaces keyword is used once, and the parent interfaces are declared in a comma-separated list.
Example:
public interface Golf extends Sports, Event
Tagging Interfaces
The most common use of extending interfaces occurs when the parent interface does not contain any methods.
Example:
package java.util; public interface EventListener {}