Anonymous Inner Classes

Definition:

  • Anonymous inner classes are a special type of inner class that does not have a name and is used to instantiate objects with certain methods or to override methods of a class or interface.

Key Points:

  • Anonymous inner classes are declared and instantiated all in one place.
  • They are often used in GUI applications for event handling or in scenarios where a one-time use implementation of an interface or subclass is required.

Use Cases:

  • Anonymous inner classes are useful when you need a short, one-time use class that either implements an interface or extends a class.
  • Commonly used in GUI frameworks like Swing for event handling, such as button clicks.
Code Example
Output
Anonymous Inner Class Running

Output

m1-A
m2-A
m3-A, Hello