Java Anonymous Class
In Java, an anonymous class is a local class without a name that is declared and instantiated in a single expression. It is typically used when you need a quick, one-time implementation of a class or interface—especially to override behavior without creating a separate .java file.
Anonymous classes help reduce boilerplate code and keep logic close to where it’s used, making programs more concise and readable.
What Is an Anonymous Class?
An anonymous class:
- Has no explicit class name
- Is declared and instantiated at the same time
- Can extend a class or implement an interface
- Is usually created for one-time use
General syntax:
Type reference = new Type() {
// override or implement methods
};
Example 1: Anonymous Class Extending a Class
In this example, an anonymous class extends a base class and overrides one of its methods.
// Base class
class Notification {
public void send() {
System.out.println("Sending a generic notification");
}
}
public class AnonymousClassDemo {
public static void main(String[] args) {
// Anonymous subclass overriding send()
Notification emailNotification = new Notification() {
@Override
public void send() {
System.out.println("Sending an email notification");
}
};
emailNotification.send();
}
}
Output:
Sending an email notification
Explanation:
- A subclass of Notification is created anonymously.
- The send() method is overridden with custom behavior.
- No separate subclass file is needed.
Example 2: Anonymous Class Implementing an Interface
Anonymous classes are commonly used to implement interfaces on the fly.
// Interface
interface Payment {
void pay(double amount);
}
public class PaymentDemo {
public static void main(String[] args) {
// Anonymous class implementing Payment
Payment cardPayment = new Payment() {
@Override
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using credit card");
}
};
cardPayment.pay(2500.00);
}
}
Output:
Paid ₹2500.0 using credit card
Explanation:
An implementation of Payment is created without naming a class.
Useful for quick behavior injection.
Example 3: Passing Behavior with an Anonymous Class
Anonymous classes are often passed as method arguments to supply custom behavior.
interface Task {
void execute();
}
class TaskRunner {
public void runTask(Task task) {
System.out.println("Starting task...");
task.execute();
System.out.println("Task finished.");
}
}
public class TaskExample {
public static void main(String[] args) {
TaskRunner runner = new TaskRunner();
// Passing anonymous class as argument
runner.runTask(new Task() {
@Override
public void execute() {
System.out.println("Processing data in background");
}
});
}
}
Output:
Starting task...
Processing data in background
Task finished.
When to Use Anonymous Classes
Use anonymous classes when:
- You need to override a method once
- You want a quick interface implementation
- You need to pass behavior as an object
- The class is small and not reused
Advantages of Anonymous Classes
- Reduces the number of separate class files
- Keeps logic close to usage
- Improves readability for small implementations
- Useful in event handling and callbacks