Interfaces are one of the most important concepts in Java and are heavily used in enterprise frameworks like Spring Boot, Hibernate, JDBC, and the Java Collections Framework.
If you've ever wondered:
- Why do we need interfaces?
- What problem do they solve?
- Why does Java use interfaces everywhere?
- How are interfaces different from classes?
Then this article is for you.
By the end of this guide, you'll understand not only what interfaces are, but also why they are one of the pillars of object-oriented programming (OOP).
What is an Interface?
There are several ways to define an interface.
Let's understand them one by one.
Definition 1: Service Requirement Specification (SRS)
An interface is a Service Requirement Specification (SRS).
It defines what services should be provided, but not how those services are implemented.
Think of it as a blueprint or contract.
Real-World Example: JDBC API
The Java team defines the JDBC API.
The JDBC API specifies methods like:
Connection getConnection();
Statement createStatement();
executeQuery();
However, Java does not implement these methods.
Instead, database vendors such as:
- MySQL
- Oracle
- PostgreSQL
- SQL Server
provide their own implementations.
Java (JDBC API)
│
▼
Defines Requirements
│
▼
Database Vendor
│
▼
Provides Implementation
This separation allows the same Java code to work with different databases by simply changing the JDBC driver.
Another Example: Servlet API
The Java team defines the Servlet API.
Application server vendors such as:
- Apache Tomcat
- Jetty
- WildFly
implement that API.
As Java developers, we simply use the API without worrying about how the server implements it.
Definition 2: Interface as a Contract
From the client's perspective, an interface defines the services the client expects.
From the service provider's perspective, it defines the services being offered.
Therefore:
An interface acts as a contract between the client and the service provider.
Real-World Example: ATM Machine
Imagine using an ATM.
The ATM screen displays options such as:
- Withdraw Money
- Deposit Money
- Balance Inquiry
- Mini Statement
Customer
│
▼
ATM Screen (Interface)
│
▼
Bank Services
The ATM screen does not tell you how the bank processes a withdrawal.
It simply tells you what services are available.
Similarly, a Java interface specifies what operations are available, not how they are performed.
Definition 3: Interface as a 100% Pure Abstract Class
Traditionally (before Java 8 introduced default and static methods), every method inside an interface was implicitly:
publicabstract
This led to the common description of an interface as a 100% pure abstract class.
For example:
interface PaymentService {
void processPayment();
void generateReceipt();
}
The compiler automatically treats these methods as:
public abstract void processPayment();
public abstract void generateReceipt();
Note: In modern Java (Java 8 and later), interfaces can also contain default, static, and private methods. However, when discussing basic interface concepts and interview fundamentals, you'll often hear the historical phrase "100% pure abstract class."
Summary Definition
An interface can be viewed as:
- A Service Requirement Specification (SRS)
- A contract between client and service provider
- A collection of method declarations that define expected behavior
Why Do We Need Interfaces?
Without interfaces, every developer might implement the same functionality differently.
Interfaces provide:
- Standardization
- Loose coupling
- Flexibility
- Extensibility
- Better maintainability
Instead of forcing everyone to follow a particular implementation, Java allows developers to agree on what should be done while leaving how it is done to the implementing class.
Interface Declaration
Declaring an interface is simple.
interface PaymentService {
void processPayment();
void generateReceipt();
}
Notice that the methods have no implementation.
The interface only specifies what services must be provided.
Implementing an Interface
A class implements an interface using the implements keyword.
interface PaymentService {
void processPayment();
void generateReceipt();
}
class CreditCardPaymentService implements PaymentService {
@Override
public void processPayment() {
System.out.println("Processing payment using Credit Card.");
}
@Override
public void generateReceipt() {
System.out.println("Receipt generated.");
}
}
Step-by-Step Explanation
Step 1
PaymentService defines two required methods.
Step 2
CreditCardPaymentService implements the interface.
Step 3
It provides implementations for both methods.
Step 4
The class can now be instantiated and used.
Rule 1: Every Interface Method Must Be Implemented
If a class implements an interface, it must provide an implementation for every abstract method.
Example:
interface PaymentService {
void processPayment();
void generateReceipt();
}
class PaymentProcessor implements PaymentService {
@Override
public void processPayment() {
System.out.println("Payment processed.");
}
}
Compile-Time Error
PaymentProcessor is not abstract and does not override
abstract method generateReceipt() in PaymentService
Why?
The interface requires two methods, but only one has been implemented.
Rule 2: Otherwise, Declare the Class as Abstract
If you don't want to implement every method immediately, declare the class as abstract.
interface PaymentService {
void processPayment();
void generateReceipt();
}
abstract class BasePaymentService implements PaymentService {
@Override
public void processPayment() {
System.out.println("Payment processed.");
}
}
The remaining method can be implemented by a subclass.
class UpiPaymentService extends BasePaymentService {
@Override
public void generateReceipt() {
System.out.println("Receipt generated.");
}
}
Rule 3: Implemented Methods Must Be Public
Interface methods are implicitly public.
Therefore, the implementing methods must also be public.
Incorrect:
interface PaymentService {
void processPayment();
}
class PaymentProcessor implements PaymentService {
void processPayment() {
System.out.println("Processing...");
}
}
Compile-Time Error
attempting to assign weaker access privileges;
was public
Correct:
class PaymentProcessor implements PaymentService {
@Override
public void processPayment() {
System.out.println("Processing...");
}
}
Common Beginner Mistakes
Mistake 1: Forgetting to Implement All Methods
Always implement every abstract method unless the class itself is abstract.
Mistake 2: Using Default Access
Incorrect:
void processPayment() {
}
Correct:
public void processPayment() {
}
Mistake 3: Confusing Interfaces with Classes
An interface is not a class.
It defines behavior, while classes provide implementations.
Best Practices
- Design interfaces around behaviors rather than implementations.
- Keep interfaces focused on a single responsibility.
- Use meaningful names such as
PaymentService,NotificationService, orOrderRepository. - Prefer interfaces when multiple implementations are expected.
- Avoid adding unrelated methods to the same interface.
Interview Questions
1. What is an interface in Java?
An interface defines a contract that specifies what methods a class must implement.
Why interviewers ask
To assess your understanding of abstraction and loose coupling.
2. Why do we need interfaces?
Interfaces promote loose coupling, flexibility, multiple implementations, and easier maintenance.
3. Can we create an object of an interface?
No.
Interfaces cannot be instantiated directly.
4. What keyword is used to implement an interface?
The implements keyword.
5. Can a class partially implement an interface?
Yes, but the class must be declared abstract.
6. Why must interface methods be implemented as public?
Because interface methods are implicitly public, and Java does not allow reducing the visibility of inherited methods.
Quick Memory Trick 🧠
Remember CARE:
C → Contract
A → Abstract behavior
R → Requirement Specification
E → Every implementation must follow it
Whenever you hear interface, think CARE—it defines a Contract that specifies Abstract behavior and Requirements that Every implementation must satisfy.
Key Takeaways
- An interface defines a contract for implementing classes.
- It specifies what should be done, not how it should be done.
- Interfaces promote loose coupling and flexibility.
- A class uses the
implementskeyword to implement an interface. - Every abstract method must be implemented unless the class is declared
abstract. - Implementing methods must be declared
public. - Interfaces are widely used in enterprise Java frameworks such as Spring, JDBC, and Servlets.
If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.
Happy Coding!
Top comments (0)