An interface defines a contract—method signatures without necessarily providing bodies (Java 8+ allows default and static methods). Classes implements one or more interfaces.
Example
interface Notifier {
void send(String message);
}
class EmailNotifier implements Notifier {
public void send(String message) {
System.out.println("Email: " + message);
}
}
Interfaces enable polymorphism without forcing a class hierarchy—similar to TypeScript interfaces or PHP interfaces.
Important interview questions and answers
- Q: Interface vs abstract class?
A: Interface for capability contracts (multiple inheritance of type); abstract class for shared state + partial implementation. - Q: Can interfaces have fields?
A: Onlypublic static finalconstants implicitly.
Self-check
- How many interfaces can a class implement?
- When would you choose an interface over an abstract class?