An interface in Java is a completely abstract type used to specify a contract or behavior that classes must implement.
An interface contains:
- Abstract methods (until Java 7)
- Default methods (Java 8+)
- Static methods (Java 8+)
- Private methods (Java 9+)
- Constants (
public static final)
A class implements an interface using the implements keyword.
-
Achieve Abstraction Defines what a class must do, not how.
-
Support Multiple Inheritance Since Java does not support multiple inheritance with classes, interfaces fill this gap.
-
Loose Coupling Code depends on interfaces, not concrete implementations.
-
Standardization Interfaces set common behavior for multiple classes.
interface Vehicle {
void start(); // abstract method
void stop(); // abstract method
}class Car implements Vehicle {
public void start() {
System.out.println("Car starts");
}
public void stop() {
System.out.println("Car stops");
}
}All variables in interfaces are:
publicstaticfinal
Example:
interface Test {
int x = 10; // public static final int x = 10;
}Before Java 8:
- Only abstract methods allowed
After updates:
| Java Version | Allowed Members |
|---|---|
| Java 8 | default methods, static methods |
| Java 9 | private methods |
| Java 7 and earlier | abstract methods only |
interface Animal {
void sound(); // implicit: public abstract
}Every implementing class must override the method.
Provide method implementation inside interface.
interface Vehicle {
default void honk() {
System.out.println("Beep!");
}
}Implementing class may override it or use it as is.
Belong to the interface itself.
interface MathUtil {
static void info() {
System.out.println("Math utilities");
}
}Usage:
MathUtil.info();Used for internal code reuse inside interfaces.
interface Demo {
private void helper() {
System.out.println("Inside private method");
}
default void test() {
helper();
}
}Not accessible outside the interface.
An interface can extend:
- Single interface
- Multiple interfaces
interface A {
void a();
}
interface B {
void b();
}
interface C extends A, B {
void c();
}Java avoids multiple inheritance with classes, but supports it with interfaces.
interface X { void run(); }
interface Y { void run(); }
class Test implements X, Y {
public void run() {
System.out.println("Running...");
}
}No ambiguity — child class provides implementation.
| Feature | Interface | Abstract Class |
|---|---|---|
| Methods | abstract, default, static, private | abstract + concrete |
| Variables | public static final | any type |
| Constructor | Not allowed | Allowed |
| Multiple inheritance | Yes | No |
| Abstraction level | Full (mostly) | Partial |
| Use cases | Common behavior across classes | Shared implementation + abstraction |
interface Payment {
void pay(double amount);
}class UPI implements Payment {
public void pay(double amount) {
System.out.println("Paid using UPI: " + amount);
}
}
class CreditCard implements Payment {
public void pay(double amount) {
System.out.println("Paid using Credit Card: " + amount);
}
}Payment p = new UPI();
p.pay(500);
p = new CreditCard();
p.pay(1000);A functional interface has exactly one abstract method.
Examples:
RunnableCallableComparatorConsumer
Can be used with lambda expressions.
Example:
@FunctionalInterface
interface Operation {
int add(int a, int b);
}
Operation o = (x, y) -> x + y;
System.out.println(o.add(5, 10));Interfaces with no methods.
Examples:
SerializableCloneableRandomAccess
Used to provide metadata or enable specific behavior.
Because only method signatures are inherited — no concrete implementation → avoids ambiguity.
Yes, but they are always public static final.
No. It can extend only interfaces.
No. It can extend another interface.
- Abstract methods → always public
- Private methods → only inside interface (Java 9+)
- Protected → not allowed
To allow backward compatibility without breaking old implementations.
-
Interfaces define a contract for classes.
-
They support multiple inheritance.
-
They can contain abstract, default, static, and private methods.
-
Variables inside interfaces are always constants.
-
Widely used in frameworks (Spring, Collections API, Threads).