How to create an interface in Java

In Java, an interface is a blueprint of a class that defines a set of methods (without implementation) that a class must implement. It is used to achieve abstraction and define a contract between different classes.

To create an interface in Java, follow these steps:

  1. Start by creating a new Java file with a .java extension and give it a meaningful name. For example, MyInterface.java.

  2. Declare the interface using the interface keyword followed by the name of the interface. For example:

    public interface MyInterface {
       
    }
    
  3. Inside the interface, define the methods that the implementing classes must override. The methods should be declared without a body, ending with a semicolon. For example:

    public interface MyInterface {
        void method1();
        int method2(String param);
    }
    

    You can define any number of methods in an interface. These methods act as a contract for the implementing classes, ensuring that they provide the necessary functionality.

  4. Optionally, you can also define constants in an interface. Constants are declared using the final and static keywords. For example:

    public interface MyInterface {
        int MAX_SIZE = 10;
        String DEFAULT_COLOR = "red";
    }
    

    Constants declared in an interface are effectively public, static, and final. They can be accessed directly from the interface or any implementing class.

  5. Save the interface file and use it in your Java programs. To implement an interface in a class, use the implements keyword followed by the interface name. For example:

    public class MyClass implements MyInterface {
        // Implement the methods of MyInterface here
    }
    

    The implementing class must provide an implementation (method body) for all the methods defined in the interface. Failure to do so will result in a compilation error.

Conclusion

Interfaces in Java provide a way to achieve abstraction and define contracts between classes. By creating interfaces, you can enforce a standard behavior across different classes.