What is an anonymous class, and in what situations is it conceptually appropriate to use one?

Last updated 5 July 2026

  • An anonymous class is an unnamed class declared and instantiated in one expression, usually a one-off implementation of an interface or abstract type.

  • Appropriate for short, single-use logic such as an event listener, callback, or comparator, where naming a class adds only noise.

  • Avoid it when logic is reused, complex, or needs isolated testing. Lambdas often replace it for functional interfaces.

  • https://www.geeksforgeeks.org/java/anonymous-inner-class-java/
All Object-Oriented Programming interview questions