Anonymous Class in Java

Anonymous Class in Java

Anonymous class in Java is a class without a name. Its name is chosen by the compiler.

It empowers us to announce and launch the class simultaneously. It resembles a nearby class aside from that it doesn’t have any name.

We can utilize it just once.

Anonymous class is otherwise called anonymous inward class in java. It is called an anonymous

internal class since it is characterized inside another class.

Step by step instructions to Create Anonymous Class in Java

Anonymous class can be made in the following two different ways.

  1. Utilizing Class
  2. Utilizing Interface

Java Anonymous Class Example Using Class

class Parent
{
 void show()
 {
  System.out.println("Parent");
 }
}
 
class Demo
{
 public static void main(String...s)
 {
  //creating anonymous inner class
  Parent p=new Parent() {
   void show()
   {
    System.out.println("Anonymous Class");
   }
  };
 
 p.show();
 }
}

Output

Anonymous Class in Java

At the point when the above code is ordered, an anonymous class is made and its name is chosen by the compiler.

This anonymous class expands Parent class and supersedes appear() technique.

In the above model, an object of an anonymous class is made and its reference is put away in reference variable p of Parent class.

I have decompiled the .class document produced by the compiler for the anonymous class. It contains the accompanying code.

import java.io.PrintStream;
 
final class Demo$1
  extends Parent
{
  void show()
  {
    System.out.println("Anonymous Class");
  }
}

Java Anonymous Class Example Using Interface

interface Parent
{
 void show();
}
 
class Demo
{
 public static void main(String...s)
 {
  //creating anonymous inner class
  Parent p=new Parent() {
   public void show()
   {
    System.out.println("Anonymous Class");
   }
  };
 
  p.show();
 }
}

At the point when the above code is arranged, an anonymous class is made and its name is chosen by the compiler.

This anonymous class executes Parent interface and abrogates appear() strategy.

In the above model, an object of an anonymous class is made and its reference is put away in reference variable p of Parent interface.

I have decompiled the .class document produced by the compiler for the anonymous class. It contains the accompanying code.

import java.io.PrintStream;
 
final class Demo$1
  implements Parent
{
  public void show()
  {
    System.out.println("Anonymous Class");
  }
}

What is the motivation behind the anonymous class in Java?

You might be feeling that the above work should likewise be possible by making a different class and afterwards expanding Parent class.

Why we have made an anonymous class? All things considered, it is snappier to make an anonymous class at that point making a different class. Anonymous

inward class is helpful when we need to supersede a modest quantity of usefulness (like one strategy) for the parent class.

It likewise makes our code increasingly succinct.

On the off chance that you discover anything absent or wrong in the above instructional

exercise, at that point please notice it by remarking underneath.

Leave a Comment

error: Alert: Content is protected!!