An interface in Java is a blueprint of behavior — a contract that any implementing class agrees to fulfill. Interfaces are often used as an alternative (or complement) to abstract classes, especially since Java doesn't support multiple inheritance with classes but does allow a class to implement multiple interfaces.
In this post, we'll cover:
- How fields behave inside an interface
- How to implement an interface
- Default and static methods (Java 8+)
- When to reach for an interface vs. an abstract class
Fields in an interface
Any variable declared inside an interface is implicitly public, static, and final — effectively a constant. Because final variables must be initialized where they're declared, this won't compile:
interface A {
String talk;
int age;
void show();
void config();
}
This will:
interface A {
String talk = "show me the way";
int age = 90;
void show();
void config();
}
Implementing an interface
A class that implements an interface must provide concrete bodies for all of its abstract methods:
class B implements A {
public void show() {
System.out.println("Inside show method");
}
public void config() {
System.out.println("Inside config method");
}
}
Putting it together
public class ApplicationWorker {
public static void main(String[] args) {
A obj = new B(); // program to the interface
obj.show();
obj.config();
System.out.println(obj.age); // 90 — inherited constant
}
}
Notice obj is declared as type A but holds a B instance. That's the real payoff of interfaces: your code can depend on the contract (A) rather than a specific implementation (B), which makes it easy to swap implementations later without touching the calling code.
Default and static methods (Java 8+)
Older Java versions required every interface method to be abstract. Since Java 8, interfaces can also define default and static methods with actual bodies:
interface A {
String talk = "show me the way";
int age = 90;
void show();
void config();
default void greet() {
System.out.println("Hello from interface A");
}
static void info() {
System.out.println("A is an interface with default behavior");
}
}
A default method gives implementing classes a working fallback they can optionally override — useful for adding new methods to an interface without breaking every class that already implements it. A static method belongs to the interface itself and is called as A.info(), not through an instance.
Interface vs. abstract class: which one to use?
A quick decision rule:
Use an interface when you're defining a capability or contract that unrelated classes might share (e.g., Comparable, Runnable) — especially if a class might need to implement more than one.
Use an abstract class when you're modeling a clear "is-a" relationship and want to share common state (fields) or partially implemented behavior among closely related subclasses.
In practice, many modern Java designs favor interfaces for flexibility, using default methods to share common behavior when needed.
Wrapping up
Interfaces let you separate what something does from how it does it. That separation is what makes code built around interfaces easier to test, extend, and swap out over time.
If you found this useful, let me know in the comments — and feel free to share other Java interface gotchas you've run into!
Top comments (0)