An interface in Java is like a contract that says what methods a class must have, without saying how they work.
Interface declaration in Java
public interface InterfaceName { // method signatures returnType methodName(parameters); }
Interfaces only declare methods; they do not provide method bodies (except default or static methods in newer Java versions).
All methods in an interface are implicitly public and abstract.
public interface Animal { void eat(); void sleep(); }
interface Vehicle { int getSpeed(); }
public interface Drawable { void draw(); default void info() { System.out.println("Drawable object"); } }
This program defines an interface Printer with a print method. ConsolePrinter class implements Printer and provides the print method. The main method creates a ConsolePrinter and calls print.
public interface Printer { void print(String message); } public class ConsolePrinter implements Printer { public void print(String message) { System.out.println(message); } } public class Main { public static void main(String[] args) { Printer printer = new ConsolePrinter(); printer.print("Hello, Interface!"); } }
Interfaces cannot have instance fields (variables), only constants (static final).
A class can implement multiple interfaces, helping with flexible design.
Since Java 8, interfaces can have default and static methods with bodies.
Interfaces define what methods a class must have, without how they work.
Use interfaces to ensure classes follow certain behaviors.
Interfaces help write flexible and reusable code by separating method declaration from implementation.