in , ,

Java Anonymous Class

Java Anonymous Class
Java Anonymous Class

Contents

Java Anonymous Class

Java Anonymous Class: In this tutorial, you will find out about Anonymous classes in Java with the help of examples.

In Java, a class can contain another class known as a nested class. It’s conceivable to make a nested class without giving any name.

A nested class that doesn’t have any name is known as a anonymous class.

An anonymous class must be characterized inside another class. Thus, it is otherwise called an anonymous internal class. Its syntax is:

class outerClass {

    // defining anonymous class
    object1 = new Type(parameterList) {
         // body of the anonymous class
    };
}

Anonymous classes usually extend subclasses or implement interfaces.

Here, Type can be

  • a superclass that an anonymous class extends
  • an interface that an anonymous class implements

The above code creates an object, object1, of an anonymous class at runtime.

Note: Anonymous classes are characterized inside an articulation. Thus, the semicolon is used toward the finish of anonymous classes to show the finish of the articulation.


Example 1: Anonymous Class Extending a Class

class Polygon {
   public void display() {
      System.out.println("Inside the Polygon class");
   }
}

class AnonymousDemo {
   public void createClass() {

      // creation of anonymous class extending class Polygon
      Polygon p1 = new Polygon() {
         public void display() {
            System.out.println("Inside an anonymous class.");
         }
      };
      p1.display();
   }
}

class Main {
   public static void main(String[] args) {
       AnonymousDemo an = new AnonymousDemo();
       an.createClass();
   }
}

Output

Inside an anonymous class.

In the above example, we have created a class Polygon. It has a single method display().

We then created an anonymous class that extends the class Polygon and overrides the display() method.

When we run the program, an object p1 of the anonymous class is created. The object then calls the display() method of the anonymous class.


Example 2: Anonymous Class Implementing an Interface

interface Polygon {
   public void display();
}

class AnonymousDemo {
   public void createClass() {

      // anonymous class implementing interface
      Polygon p1 = new Polygon() {
         public void display() {
            System.out.println("Inside an anonymous class.");
         }
      };
      p1.display();
   }
}

class Main {
   public static void main(String[] args) {
      AnonymousDemo an = new AnonymousDemo();
      an.createClass();
   }
}

Output

Inside an anonymous class.

In the above example, we have made an anonymous class that actualizes the Polygon interface.


Advantages of Anonymous Classes

In Anonymous classes, objects are made at whatever point they are required. That is, objects are made to play out some particular tasks. For instance,

Object = new Example() {
   public void display() {
      System.out.println("Anonymous class overrides the method display().");
   }
};

Here, an object of the anonymous class is created dynamically when we need to override the display() method.

Anonymous classes also help us to make our code concise.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Nested Static Class

Java Nested Static Class

Java Singleton

Java Singleton