elibraryportal Logo

Anonymous Inner class in Java

→ The anonymous class is also known as an anonymous inner class that means An inner class that is declared without a name is known as an anonymous class in java.

→ Inner class in java, which has no name is called Anonymous Inner class.

Important point:

  • Anonymous class has no name
  • It can be instantiated only once
  • It is usually declared inside a method or a code block, curly braces ending with a semicolon.
  • It does not have a constructor simply because it does not have a name.
  • It cannot be static.
  • It is accessible only at the point where it is defined.

→ Create Anonymous inner class in the following way :-

  • extending a class.
  • extending abstract class.
  • implementing interface

1) Example by extending a class:


Anonymous inner extending a class Example

2) Example by extending abstract class:


Anonymous inner abstract class Example

3) Example by implementing interface:


Anonymous inner implementing interface Example