Introduction
An Interface is one of the most powerful features of Object-Oriented Programming (OOP) in Java. It is used to define a contract that classes must follow. Instead of providing complete implementations, an interface specifies what a class should do, leaving how it does it to the implementing classes.
Interfaces play a crucial role in achieving abstraction, multiple inheritance, and loose coupling.
Since Java 8 and Java 9, interfaces have become even more powerful by supporting default methods, static methods, and private methods, reducing the need for abstract classes in many scenarios.
What is an Interface?
An interface is a reference type in Java that defines a set of methods that implementing classes must provide.
Traditionally, interface methods were abstract. Since Java 8, interfaces can also contain:
- Abstract methods
- Default methods
- Static methods
Since Java 9, they can also include:
- Private methods
- Private static methods
Definition:
An interface is a contract that specifies a set of methods which implementing classes must define.
Why Do We Need Interfaces?
Without interfaces, different classes performing similar tasks would have no common contract.
Example without an interface:
class CreditCard {
void pay() {
System.out.println("Credit Card Payment");
}
}
class UPI {
void pay() {
System.out.println("UPI Payment");
}
}
//Although both classes have a pay() method, there is no common type to work with Using an interface:
interface Payment {
void pay();
}
Now every payment method follows the same contract.
Syntax
interface InterfaceName {
void methodName();
}
A class implements an interface using the implements keyword.
class ClassName implements InterfaceName {
@Override
public void methodName() {
}
}
Implementing an Interface
Example:
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}
Output
The Dog class provides the implementation for the sound() method declared in the interface.
Interface Variables
Variables declared in an interface are automatically:
- public
- static
- final
Example:
interface Constants {
int MAX_USERS = 100;
}
Usage:
System.out.println(Constants.MAX_USERS);
The value cannot be modified because it is a constant.
Multiple Interface Implementation
One of the biggest advantages of interfaces is that a class can implement multiple interfaces.
Example:
interface Camera {
void takePhoto();
}
interface MusicPlayer {
void playMusic();
}
class Smatphone implements Camera, MusicPlayer {
@Override
public void takePhoto() {
System.out.println("Photo Captured");
}
@Override
public void playMusic() {
System.out.println("Playing Music");
}
}
This allows Java to support multiple inheritance of type, avoiding the ambiguity associated with multiple inheritance of classes.
Default Methods (Java 8)
Default methods allow interfaces to provide method implementations.
Example:
interface Vehicle {
default void start() {
System.out.println("Vehicle Started");
}
}
class Car implements Vehicle {
}
Usage:
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Output
Default methods make it easier to extend interfaces without breaking existing implementations.
Static Methods (Java 8)
Interfaces can contain static methods.
Example:
interface Calculator {
static int square(int number) {
return number * number;
}
}
Usage:
System.out.println(Calculator.square(5));
Output
Static methods belong to the interface itself and are called using the interface name.
Private Methods (Java 9)
Private methods help reduce code duplication inside default methods.
Example:
interface Message {
default void show() {
print();
}
private void print() {
System.out.println("Hello Java");
}
}
Private methods cannot be accessed outside the interface.
Functional Interface
A Functional Interface contains exactly one abstract method.
Example:
@FunctionalInterface
interface Greeting {
void sayHello();
}
Functional interfaces are widely used with Lambda Expressions and the Stream API.
Examples from the Java API include:
- Runnable
- Callable
- Comparator
- Predicate
- Consumer
- Supplier
Interface vs Abstract Class
| Interface | Interface |
|---|---|
| Uses the interface keyword | Uses the abstract keyword |
| Supports multiple implementation | Supports single inheritance |
| No constructors | Can have constructors |
| Variables are constants | Can have instance variables |
| Used to define a contract | Used to share common implementation |
| Can have default and static methods | Can have concrete and abstract methods |
Real-Life Example
Consider a Payment Gateway.
interface Payment {
void pay(double amount);
}
class CreditCardPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid using Credit Card: " + amount);
}
}
class UpIPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid using UPI: " + amount);
}
}
public class Main {
public static void main(String[] args) {
Payment payment = new CreditCardPayment();
payment.pay(5000);
payment = new UPIPayment();
payment.pay(2500);
}
}
Output
Paid using UPI: 2500.0
The application works with the Payment interface without depending on a specific payment implementation.
Advantages of Interfaces
Interfaces provide many benefits:
- Achieve abstraction.
- Support multiple inheritance of type.
- Promote loose coupling.
- Improve code reusability.
- Simplify testing and mocking.
- Make applications easier to maintain.
- Encourage modular design.
- Allow applications to be extended without changing existing code.
Best Practices
Follow these best practices when using interfaces:
- Use interfaces to define capabilities or contracts.
- Keep interfaces focused on a single responsibility.
- Prefer meaningful interface names such as PaymentProcessor or Printable.
- Use default methods only when necessary.
- Avoid placing unrelated methods in the same interface.
- Use the @FunctionalInterface annotation for functional interfaces.
- Program to interfaces instead of concrete classes whenever possible.
Common Mistakes
Beginners often make these mistakes:
- Confusing interfaces with abstract classes.
- Trying to instantiate an interface directly.
- Forgetting to implement all abstract methods.
- Overusing default methods.
- Treating interface constants as mutable variables.
- Creating large interfaces that violate the Interface Segregation Principle (ISP).
- Using an abstract class when an interface is more appropriate.
Conclusion
A Java Interface is a powerful language feature that defines a common contract for classes while promoting abstraction, loose coupling, and code reusability. By programming against interfaces rather than concrete implementations, developers can build applications that are easier to extend, test, and maintain.
Modern Java interfaces support default methods, static methods, private methods, and functional interfaces, making them more versatile than ever.
Java Interface – Interview Questions
Q 1: What is an interface in Java?
Q 2: Which keyword is used to implement an interface?
Q 3: Can an interface have methods with body?
Q 4: Does Java support multiple inheritance using interface?
Q 5: Can an interface have variables?
Java Interface – Objective Questions (MCQs)
Q1. Which keyword is used to declare an interface in Java?
Q2. Which of the following statements about interfaces in Java is true?
Q3. How can a class use an interface in Java?
Q4. What is the default access modifier for interface methods?
Q5. From Java 8 onwards, which type of methods can be defined in an interface?