Anonymous Class – Some quick facts

  • An Anonymous class is a class not given any name.
  • It is declared and instantiated in a single line
  • You should consider using anonymous when you need to create a class that will be instantiated only once.
  • An Anonymous class can implement an abstract class or interface without using extends or implements keyword, where it will be implementing the methods inside the body
  • An anonymous class cannot have a constructor. So you cant send any parameters
  • An anonymous class can access any variables visible to the block within which it is declared.
  • An anonymous class can also access the methods of the class that contains it.

Runnable r = new Runnable(){
public void run(){
//implementation for run
}
}

image

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top