Anonymous Classes in Java

Introduction to Anonymous Classes in Java

Anonymous classes in Java are inner classes that are declared and instantiated without a name. They are typically used to implement interfaces or extend abstract classes. Anonymous classes can be useful for a variety of purposes, such as:

  • Implementing event handlers
  • Creating temporary objects
  • Overriding methods of an abstract class
  • Implementing callback methods

To create an anonymous class, you use the `new` keyword followed by the name of the interface or abstract class that you want to implement or extend. You can then provide a list of arguments to the constructor of the anonymous class. The following code shows how to create an anonymous class to implement the `Runnable` interface:


Runnable runnable = new Runnable() {
  @Override
  public void run() {
    // Code to execute in the background thread
  }
};

The anonymous class in the above example overrides the `run()` method of the `Runnable` interface. This method is executed when the thread is started.

Anonymous classes can also be used to extend abstract classes. The following code shows how to create an anonymous class to extend the `AbstractList` class:


AbstractList list = new AbstractList() {
  @Override
  public int size() {
    return 0;
  }

  @Override
  public String get(int index) {
    return null;
  }
};

The anonymous class in the above example overrides the `size()` and `get()` methods of the `AbstractList` class. These methods must be implemented by all concrete subclasses of `AbstractList`.

Anonymous classes can be a powerful tool for writing concise and efficient Java code. However, they can also be difficult to debug and read. It is important to use them carefully and to document them well.

Benefits of using anonymous classes in Java:

  • Conciseness: Anonymous classes can help to make your code more concise by eliminating the need to create separate named classes for simple tasks.
  • Flexibility: Anonymous classes can be used to implement interfaces or extend abstract classes without having to create separate named classes. This can be useful for tasks such as implementing event handlers or creating temporary objects.
  • Modularity: Anonymous classes can help to make your code more modular by encapsulating code that is specific to a particular task.

Drawbacks of using anonymous classes in Java:

  • Debugability: Anonymous classes can be difficult to debug, because they do not have a name and they can be nested several levels deep.
  • Readability: Anonymous classes can make your code less readable, especially if they are complex or nested.

Overall, anonymous classes can be a powerful tool for writing concise and efficient Java code. However, it is important to use them carefully and to document them well.

Exercises

Code for demonstrating the use of Anonymous Classes in Java.


  package practiceproject;
  import java.io.*;
  public class democlass{
    
      public static void main(String[] args){
          
      //anonymous class example 
      employee p = new employee() {
        public void display()
        {
          System.out.println("I am the professor");
        }
      };
      p.display();
      }
  }
  abstract class employee
  {
    public abstract void display();
  } 

Code for demonstrating the use of Anonymous Classes with an Interface


  package practiceproject;
  import java.io.*;
  public class democlass{
    
      public static void main(String[] args){
          
        employee e = new employee() {
          public void display()
          {
            System.out.println("I am the anonymous professor");
          }
        };
        e.display();
      }
  }
  
  interface employee
  {
    void display();
  }