Interfaces
An interface in Java defines a contract that a class must follow. It specifies what a class can do, not how it does it. Interfaces are fundamental to abstraction, loose coupling, multiple inheritance, and API design. This is a very high-frequency interview topic, especially when compared with abstract classes.
What Is an Interface?
- A blueprint that contains method declarations (contracts)
- Implemented by classes using the implements keyword
- Supports multiple inheritance
- Promotes loose coupling
interface Payment {
void pay();
}
Why Interfaces Are Needed
- Achieve full abstraction
- Support multiple inheritance
- Enable plug-and-play design
- Improve testability and flexibility
- Foundation of frameworks (Spring, JDBC, Collections)
Basic Syntax
interface InterfaceName {
returnType methodName();
}
class ClassName implements InterfaceName {
public returnType methodName() {
// implementation
}
}
Simple Interface Example
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
Animal a = new Dog();
a.sound();
✔ Runtime polymorphism
✔ Loose coupling
Key Rules of Interfaces (Very Important)
- Methods Are public and abstract by Default
interface Test {
void show(); // public abstract implicitly
}
- Variables Are public static final by Default
interface Config {
int MAX = 100; // constant
}
- ✔ Must be initialized
- ✔ Cannot be changed
- Interface Cannot Be Instantiated
// Test t = new Test(); // ❌
- A Class Must Implement All Interface Methods
class A implements Test {
public void show() { }
}
✔ Otherwise, class must be declared abstract
Multiple Inheritance Using Interfaces (Key Advantage)
interface A {
void show();
}
interface B {
void display();
}
class C implements A, B {
public void show() { }
public void display() { }
}
✔ Solves the diamond problem safely
✔ One of Java’s strongest design features
Interface Reference and Polymorphism
Payment p = new CreditCardPayment();
p.pay();
- Reference type → Interface
- Object type → Implementation class
- Method call resolved at runtime
Interface Inheritance (extends)
An interface can extend another interface.
interface A {
void show();
}
interface B extends A {
void display();
}
✔ Supports multiple inheritance between interfaces
Java 8+ Interface Enhancements (Interview Favorite)
- Default Methods
Allow method implementation inside interface.
interface Vehicle {
default void start() {
System.out.println("Vehicle starts");
}
}
✔ Prevents breaking existing implementations
- Static Methods
interface Utility {
static void help() {
System.out.println("Helping");
}
}
✔ Called using interface name only
- Functional Interfaces (Preview)
@FunctionalInterface
interface Calculator {
int add(int a, int b);
}
✔ Used with Lambda expressions
Interface vs Abstract Class (Interview Favorite)
| Feature | Interface | Abstract Class |
|---|---|---|
| Abstraction | Full | Partial |
| Methods | Abstract + default | Abstract + concrete |
| Variables | Constants only | Instance variables |
| Constructors | ❌ No | ✅ Yes |
| Multiple inheritance | ✅ Yes | ❌ No |
| Use case | Contract / capability | Base class |
When to Use Interfaces
- When multiple inheritance is required
- When defining a contract
- When building APIs/frameworks
- When behavior varies across implementations
- When loose coupling is needed
When NOT to Use Interfaces
- When shared state is required
- When common implementation is large
- When constructor logic is needed
Common Beginner Mistakes
- Forgetting methods are public
- Trying to create objects of interface
- Not implementing all methods
- Confusing interface with abstract class
- Overusing default methods
Interview-Ready Answers
Short Answer
An interface defines a contract that a class must implement.
Detailed Answer
In Java, an interface provides full abstraction by declaring methods without implementation. Classes implement interfaces using the implements keyword. Interfaces support multiple inheritance, polymorphism, and are widely used for designing flexible and loosely coupled systems.
Key Takeaway
Interfaces define “what to do,” not “how to do it.” They are the backbone of scalable, extensible, and testable Java applications.