Lesson 27
Anonymous Inner Class
An anonymous inner class has no name and is declared and instantiated in one statement. Used for one-time method overriding or interface implementation.
Java Track
Save progress as you learn.
61 lessons
Lesson content
A Java Anonymous Inner Class is an inner class without a name that is declared and instantiated at the same time. It is commonly used when you need to override a method or implement an interface for one-time use without creating a separate class.
What is an Anonymous Inner Class?
- Does not have a class name.
- Is created and used in a single statement.
- Is generally used for one-time object creation.
- Can extend a class or implement an interface.
Definition List
- Anonymous Inner Class: An inner class without a name that is declared and instantiated in a single statement.
- Inner Class: A class declared inside another class.
- Abstract Class: A class that cannot be instantiated and may contain abstract methods.
- Method Overriding: Providing a new implementation of a method inherited from a parent class.
- Object: An instance of a class that contains data and behavior.
Why Use Anonymous Inner Classes?
- A class is required only once.
- You want to override a method quickly.
- Creating a separate class would increase code unnecessarily.
- Implementing interfaces or abstract classes for temporary use.
Benefits
- Reduces code length.
- Improves readability for simple implementations.
- Useful for event handling and callbacks.
- Eliminates the need for creating separate class files.
Syntax of an Anonymous Inner Class
Extending a Class
ClassName object = new ClassName() {
// Method implementation
};Implementing an Interface
InterfaceName object = new InterfaceName() {
// Method implementation
};Example: Java Anonymous Inner Class
abstract class testDemo {
abstract void display();
}
class outerDemo {
public void outerDisplay() {
testDemo o = new testDemo() {
@Override
public void display() {
System.out.println("Test Display");
}
};
o.display();
}
}
public class anonymousInner {
public static void main(String[] args) {
outerDemo o = new outerDemo();
o.outerDisplay();
}
}How the Program Works
- abstract class testDemo contains abstract method display(). Cannot be instantiated directly.
- A new object of testDemo is created and display() is overridden immediately — no separate class needed.
- o.display() calls the overridden method.
Output
Test DisplayAnonymous Inner Class vs Normal Inner Class
- Class Name — Anonymous: No. Normal: Yes.
- Reusability — Anonymous: No. Normal: Yes.
- Separate Class Definition — Anonymous: Not Required. Normal: Required.
- Object Creation — Anonymous: Immediate. Normal: Separate.
- Best Use Case — Anonymous: One-time use. Normal: Reusable functionality.
Real-World Example
In Java GUI applications, anonymous inner classes are commonly used for button click event handling:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
});Advantages of Anonymous Inner Classes
- Less Code: No need to create a separate class file.
- Faster Development: Quickly implement interfaces or abstract classes.
- Better Readability: Useful when the implementation is small and used only once.
- Convenient Event Handling: Commonly used in Java GUI applications and event listeners.
Limitations of Anonymous Inner Classes
- Cannot have constructors.
- Cannot be reused elsewhere.
- Difficult to maintain if the implementation becomes large.
- Can reduce readability when excessive code is placed inside them.
FAQ
1. What is an Anonymous Inner Class in Java?
An Anonymous Inner Class is an unnamed class that is declared and instantiated at the same time for one-time use.
2. Can an Anonymous Inner Class extend a class?
Yes. An Anonymous Inner Class can extend a class or implement an interface.
3. What is the main advantage of an Anonymous Inner Class?
The main advantage is reducing code by avoiding the creation of a separate class for a one-time implementation.
Where Java is used
Java in real-world development
Anonymous Inner Class
Unnamed class created and used in one statement. No separate class file needed.
One-Time Use
Ideal when a class implementation is only needed once in the program.
Extends or Implements
Can extend an abstract class or implement an interface on the spot.
No Constructor
Anonymous inner classes cannot define constructors.
Event Handling
Widely used in Java GUI apps for button clicks and action listeners.
Limitation
Cannot be reused. Hard to maintain if implementation grows large.