An Interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. It is a mechanism to achieve 100% abstraction and multiple inheritance in Java. Think of an interface as a formal agreement: "Any class that implements this interface MUST provide these specific behaviors."
Interfaces are used to achieve three primary goals in software engineering:
implement multiple interfaces. This solves the "Diamond Problem."When you declare an interface, Java automatically applies several modifiers to your members. You don't need to type them, but the compiler adds them for you:
| Member Type | Implicit Modifiers |
|---|---|
| Variables | public static final (They are constants) |
| Methods | public abstract (They have no body) |
One of the biggest limitations of Java classes is that a child can have only one parent. However, a class can be "multitalented" by implementing several interfaces. For example, a Smartphone class can implement Camera, Phone, and WebBrowser.
class Smartphone implements Camera, Phone, WebBrowser { ... }
This allows the Smartphone to inherit behaviors from all three sources without the confusion of multiple class inheritance.
Before Java 8, interfaces could not have method bodies. This made it hard to update interfaces used in large libraries. Java 8 introduced:
default keyword and do have a body. They allow you to add new functionality to interfaces without breaking the classes that already implement them.This example demonstrates how different devices can be controlled through a unified interface.
Choosing between an interface and an abstract class is a critical design decision:
| Feature | Interface | Abstract Class |
|---|---|---|
| Abstraction | 100% (mostly) | 0 to 100% (Partial) |
| Inheritance | Multiple can be implemented. | Only one can be extended. |
| Variables | Only constants (static final). | Can have instance variables. |
| Constructors | No constructors allowed. | Can have constructors. |
Serializable, Cloneable). It simply "marks" a class as having a certain property.Runnable). These are the foundation for Lambda Expressions.Q: Can an interface implement another interface?
A: No. Interfaces extend other interfaces. A class implements an interface.
Q: Why are interface variables static and final?
A: Interfaces cannot be instantiated, so variables must be static to be accessed. They are final to ensure that the "contract" doesn't change—the interface defines behavior, not state.
Q: What are Private Methods in Interfaces?
A: Introduced in Java 9, these allow you to share common code between multiple default or static methods within the same interface without exposing that code to the public.
Interfaces are the backbone of professional Java APIs. They allow for a "Pluggable Architecture" where you can design systems that are flexible, modular, and easy to test. By mastering interfaces, you move beyond writing simple scripts and start designing robust, decoupled software systems.
Next: Review Java Encapsulation →