100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is an Anonymous Class in OOP?

Learn what an anonymous class is in OOP -- one-off interface implementations, captured variables, and lambda comparisons -- in Java.

mediumQ95 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

An anonymous class is a class declared and instantiated in a single expression, with no name of its own, typically used to provide a one-off implementation of an interface or abstract class inline where it’s needed.

Instead of writing a separate named class file just to override one or two methods once, an anonymous class lets you supply the implementation directly at the point of use, immediately after 'new SomeType() { ... }'. Under the hood the compiler still generates a real class file (with a synthetic name like Outer$1), and if declared inside an instance method it behaves like an inner class, capturing an implicit reference to the enclosing instance and any effectively-final local variables it uses. Anonymous classes are commonly used for event listeners, comparators, and simple callback implementations, though in modern Java, lambda expressions have replaced many of these use cases for functional interfaces with a single abstract method.

  • Avoids creating a separate named class file for a one-off implementation
  • Keeps the implementation visually close to where it is used
  • Can capture local variables and outer state for the callback
  • Reduces boilerplate for simple interface or abstract-class implementations

AI Mentor Explanation

A substitute fielder called on for a single over doesn’t get a named, permanent squad position -- they’re given a one-time role, do the job for that over, and then it’s over, with no lasting record of them as a distinct player type. That’s an anonymous class: a throwaway implementation created for one specific moment, without ever being given a proper name in the roster. The team still uses them exactly like any fielder, just without formally naming the role.

Step-by-Step Explanation

  1. Step 1

    Identify a one-off implementation need

    You need to implement an interface or extend an abstract class exactly once, at one call site.

  2. Step 2

    Write new Type() { ... } inline

    Provide the method bodies directly inside the braces, right where the object is being created.

  3. Step 3

    Compiler generates a synthetic class

    A hidden class file like Outer$1 is produced behind the scenes, even though it has no source-level name.

  4. Step 4

    Captures effectively-final locals

    Local variables used inside must be effectively final, since the anonymous class captures a copy at creation time.

What Interviewer Expects

  • Correct definition: unnamed class defined and instantiated at the point of use
  • Understanding that anonymous classes can implement interfaces or extend abstract/concrete classes
  • Awareness of the effectively-final rule for captured local variables
  • Knowledge that lambdas replace many but not all anonymous-class use cases

Common Mistakes

  • Believing anonymous classes have literally no compiled class file at all
  • Trying to capture and modify a non-final local variable inside one
  • Assuming an anonymous class can implement more than one interface at a time
  • Confusing anonymous classes with lambda expressions in every case (lambdas only work for functional interfaces)

Best Answer (HR Friendly)

An anonymous class lets you define and instantiate a class in the same spot, without giving it a name, which is handy when you only need one specific implementation of an interface or abstract class just once. It keeps small, one-off logic close to where it’s used instead of cluttering the codebase with a separate file for something used exactly one time.

Code Example

Anonymous class implementing an interface
interface ClickHandler {
    void onClick(String elementId);
}

public class Button {
    void registerHandler(ClickHandler handler) {
        handler.onClick("submit-btn");
    }

    void setup() {
        // Anonymous class: no name, defined and used right here
        registerHandler(new ClickHandler() {
            @Override
            public void onClick(String elementId) {
                System.out.println("Clicked: " + elementId);
            }
        });
    }
}

Follow-up Questions

  • How does an anonymous class differ from a lambda expression?
  • What name does the compiler assign to a generated anonymous class file?
  • Why must local variables captured by an anonymous class be effectively final?
  • Can an anonymous class extend a concrete class instead of implementing an interface?

MCQ Practice

1. An anonymous class is best described as?

An anonymous class has no source-level name and is defined and instantiated at the exact point it is used.

2. What must be true of a local variable used inside an anonymous class?

Anonymous classes capture a copy of local variables at creation time, so those variables must be effectively final.

3. Which of these can an anonymous class NOT do?

An anonymous class can implement exactly one interface or extend exactly one class -- never multiple interfaces at once.

Flash Cards

Anonymous class in one line?A class defined and instantiated in one expression, with no name, for a one-off implementation.

What does the compiler generate for it?A real class file with a synthetic name like Outer$1.

Rule for captured local variables?They must be effectively final.

Modern alternative for single-method interfaces?Lambda expressions, for functional interfaces specifically.

1 / 4

Continue Learning