anonymous class in java

Java Anonymous Class with Example

In this tutorial, you will learn about Java Anonymous Class with an example in detail.

What is anonymous class in java?

You can use anonymous classes to make your code more concise. They allow you to simultaneously declare and instantiate a class. They’re similar to local classes, but they don’t have a name. If you only need to use a local class once, use them.

  • A nested class in Java is a class that contains another class. You can create a nested class without giving it a name.
  • An anonymous class is a nested class that does not have a name.
  • A class that is anonymous must be defined within another class. As a result, it’s also referred to as an anonymous inner class.

Its syntax is as follows:

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

In most cases, anonymous classes extend subclasses or implement interfaces.

Note: In simple terms, an anonymous inner class in Java is a class that has no name. If you need to override a class or interface method, this is the method to use.
–There are two ways to make an anonymous inner class in Java:
class (may be abstract or concrete).
Interface

Example : Anonymous Class Extending a Class

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

class AnonymousSample {
   public void createClass() {

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

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

Output

Inside an anonymous class.

In the preceding example, we created the Square class. It only shows a single method ().

  • Then we made an anonymous class that extends Square and extends the display() method.
  • When we run the program, an anonymous class object named s is created. The anonymous class’s display() method is then called by the object.

Example: Anonymous Class Implementing an Interface

interface Square {
   public void display();
}

class AnonymousSample {
   public void createClass() {

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

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

Output

Inside an anonymous class.

We’ve created an anonymous class that implements the Square interface in the preceding example.

What is anonymous block in Java?

In Java, an anonymous block is a unique member of a class. It has no names and represents statements that are shared by all of the class constructors.

Advantages of Anonymous Classes

  • Objects are created whenever they are needed in anonymous classes. Objects, in other words, are created to perform specific tasks.

As an example:

Object = new Example() {
public void display() {
System.out.println(“Anonymous class overrides the method display().”);
}
};
  • When we need to override the display() method, we create a dynamic object of the anonymous class.
  • Anonymous classes also aid in the clarity of our code.
Note: Anonymous Classes are subject to certain restrictions.  Anonymous classes, like local classes, cannot be made public, private, protected, or static. It is not possible to define a constructor for an anonymous class because it has no name. You must use a local class instead of a global class if your class requires a constructor.

You may like:

Java Inheritance with Example | Types of inheritance

Java final Keyword with Example

Not Equal Example in Java

Java Polymorphism with Example

Hope this article will guide you to recognize all about Java Anonymous Class with an example that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

anonymous class in java, anonymous inner anonymous inner class in java example, java anonymous class example,

Leave a Reply