Interfaces in Java
Learn how interfaces define contracts, support abstraction, enable multiple inheritance of type, and help build loosely coupled Java applications.
What is an Interface?
An interface in Java is a reference type that defines a contract that implementing classes agree to follow.
Interfaces are commonly used to achieve abstraction, polymorphism, loose coupling, and multiple inheritance of type.
interface CiitAccount { {
void login();
void logout();
}
Implementing an Interface
A class implements an interface using the
implements keyword.
interface CiitAccount {
void login();
void logout();
}
class StudentAccount implements CiitAccount {
public void login() {
System.out.println("Student logged in : Accessible online classes..🤓👀");
}
public void logout() {
System.out.println("Student logged out safely..🫥");
}
}
Using an Interface Reference
CiitAccount account = new StudentAccount();
account.login();
accoutn.logout();
Student logged in : Accessible online classes..🤓👀
Student logged out safely..🫥
The reference type is Vehicle, while the actual
object is Car. This is interface-based runtime
polymorphism.
Abstract Methods in Interfaces
A traditional interface method without a body is implicitly
public and abstract.
interface CiitExam {
void evaluate();
}
class LabExam implements CiitExam {
@Override
public void edit() {
System.out.println("Lab Exam Evaluated: Checking practical coding assignments.");
}
}
Variables in Interfaces
Fields declared in an interface are implicitly
public, static, and
final.
interface Configuration {
int MAX_USERS = 100;
}
class Application {
void showLimit() {
System.out.println(
Configuration.MAX_USERS
);
}
}
Implementing Multiple Interfaces
A Java class can implement multiple interfaces.
interface Printable {
void print();
}
interface Scannable {
void scan();
}
class Printer implements Printable, Scannable {
public void print() {
System.out.println("Printing CIIT study materials and notes 🤓👀.");
}
public void scan() {
System.out.println("Scanning CIIT student admission documents.");
}
}
This is how Java supports multiple inheritance of type without allowing a class to extend multiple classes.
Interface Inheritance
One interface can extend another interface using
extends.
interface Animal {
void eat();
}
interface Pet extends Animal {
void play();
}
class Dog implements Pet {
public void eat() {
System.out.println("Dog eats");
}
public void play() {
System.out.println("Dog plays");
}
}
Multiple Interface Inheritance
An interface can extend multiple interfaces.
interface Camera {
void takePhoto();
}
interface GPS {
void locate();
}
interface SmartPhone extends Camera, GPS {
void call();
}
Default Methods
Since Java 8, interfaces can contain
default methods with an implementation.
interface Vehicle {
void start();
default void horn() {
System.out.println("Beep beep");
}
}
class Car implements Vehicle {
public void start() {
System.out.println("Car started");
}
}
Car car = new Car();
car.start();
car.horn();
Static Methods in Interfaces
Interfaces can also contain static methods. These methods belong to the interface itself.
interface MathUtil {
static int square(int number) {
return number * number;
}
}
int result = MathUtil.square(5);
System.out.println(result);
25
Private Methods in Interfaces
Since Java 9, interfaces can contain private methods. They are useful for sharing implementation logic between default and static methods inside the interface.
interface Logger {
default void info(String message) {
log("INFO", message);
}
default void error(String message) {
log("ERROR", message);
}
private void log(
String level,
String message
) {
System.out.println(
level + ": " + message
);
}
}
Functional Interfaces
A functional interface contains exactly one abstract method. It can be used with lambda expressions.
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
Calculator addition =
(a, b) -> a + b;
System.out.println(
addition.calculate(10, 20)
);
30
Interface vs Abstract Class
| Feature | Interface | Abstract Class |
|---|---|---|
| Keyword | interface | abstract class |
| Class Inheritance | Class implements it | Class extends it |
| Multiple Type Inheritance | Supported | Not through classes |
| Constructors | No | Yes |
| Instance Fields | No ordinary instance fields | Yes |
| Default Methods | Yes | Not applicable |
Example: Payment System
interface Payment {
void pay(double amount);
}
class CreditCardPayment implements Payment {
public void pay(double amount) {
System.out.println(
"Credit card payment: " + amount
);
}
}
class UpiPayment implements Payment {
public void pay(double amount) {
System.out.println(
"UPI payment: " + amount
);
}
}
class PaymentService {
void processPayment(
Payment payment,
double amount
) {
payment.pay(amount);
}
}
PaymentService service =
new PaymentService();
service.processPayment(
new CreditCardPayment(),
1500
);
service.processPayment(
new UpiPayment(),
800
);
The service depends on the Payment abstraction,
not on a specific payment implementation. This improves
flexibility and reduces coupling.
Benefits of Interfaces
- Provides a clear contract between components.
- Supports abstraction.
- Enables runtime polymorphism.
- Supports multiple inheritance of type.
- Helps achieve loose coupling.
- Makes systems easier to test and extend.
Interface Best Practices
- Keep interfaces focused and cohesive.
- Prefer meaningful method names that clearly express the contract.
- Avoid unnecessarily large interfaces.
- Use interfaces when multiple implementations are expected or when abstraction provides value.
- Program against interfaces when loose coupling is beneficial.
Interview Questions
Summary
Interfaces define contracts and are a key part of abstraction and polymorphism in Java. A class can implement multiple interfaces, while interfaces can extend other interfaces. Modern Java also supports default, static, and private interface methods.