How to Implement Anonymous Class in Java

Last updated on Jun 17,2021 8.1K Views

How to Implement Anonymous Class in Java

edureka.co

Anonymous classes let you write small code and let you declare and instantiate class at the same time. These classes do not have names, it is used when you want to use any class only once. It is an important aspect of Java Programming. Let’s understand what is Anonymous Class in Java in the following order:

 

Declaring Anonymous Classes

Normally we create a class i.e we declare class but, anonymous classes are expressions which means that we define the anonymous class in another expression. In simple words, the anonymous inner class is a class without names and only one object is created.

Anonymous class is useful when we have to create an instance of the object with overloading methods of a class or interface without creating a subclass of class.

Anonymous can be created in two ways:

In the anonymous class we can declare the following:

 

Syntax of Anonymous Class in Java

The syntax of an anonymous class is just like constructor except that there is a class definition in the block as shown in the snippet below:

// AnonymousClass = interface,abstract/concrete class.
AnonymousClass t = new AnonymousClass()
{
  // methods and fields
  public void someMethod()
  {
     //code goes here
   }  
}

Let’s take a look at the following example:

The following example, HelloAnonymousClass, uses anonymous classes in the initialization statements of the local variables greetSomeone but, uses a local class for the initialization of the variable greetWorld:

public class HelloAnonymousClass {
interface HelloWorld {
public void sayHello();
public void sayHelloTo(String someone);
}
public void sayHello() {
class GreetWorld implements HelloWorld {
String name = "world";
public void sayHello() {
sayHelloTo("world");
}
public void sayHelloTo(String someone) {
name = someone;
System.out.println("Hello " + name);
}
}
HelloWorld greetWorld = new GreetWorld();
HelloWorld greetSomeone = new HelloWorld() {
String name = "jon";
public void sayHello() {
sayHelloTo("Jon");
}
public void sayHelloTo(String someone) {
name = someone;
System.out.println("hola " + name);
}
};
greetWorld.sayHello();
greetSomeone.sayHelloTo("Doe");
}
public static void main(String... args) {
HelloAnonymousClass myApp = new HelloAnonymousClass();
myApp.sayHello();
}
}

As we have seen that anonymous class is an expression the syntax is just like constructor except there is a class definition in the block. Consider the instantiation of the greetSomeone object:

HelloWorld greetSomeone = new HelloWorld() {
String name = "jon";
public void sayHello() {
sayHelloTo("Jon");
}
public void sayHelloTo(String someone) {
name = someone;
System.out.println("hola " + name);
}
}

The anonymous class is composed of the following:

Anonymous class should be part of statements.

In the above example, the anonymous class expression is part of the statement which is initiated by greetSomeone.

 

Ways to Create Anonymous Class in Java

There are 3 ways to create Inner Class in Java

We can create an anonymous inner class by extending other class, suppose we have to create a thread by using Thread class we can create an anonymous inner class instead of creating a separate class.

//Program to illustrate Anonymous Inner class by extending other class
class AnonymousThreadClass {
public static void main(String[] args) {
//Anonymous Inner class that extends Thread class
Thread t = new Thread() {
public void run() {
System.out.println("Child!");
}
};
t.start();
System.out.println("Parent!");
}
}

Output:

Parent!

Child!

 

We can also create an anonymous inner class by implementing the interface. Now, as we created an inner class by extending class similarly we can create a class that implements an interface.

//Program to illustrate Anonymous Inner class by implementing interface
class AnonymousThreadClass
{
public static void main(String[] args)
{
//Anonymous Inner class that implements interface
Runnable r = new Runnable()
{
public void run()
{
System.out.println("Child!");
}
};
Thread t = new Thread(r);
t.start();
System.out.println("Parent!");
}
}

Output:

Parent!

Child!

 

To understand syntax lets look at the example below:

//Program to illustrate Anonymous Inner class by argument
class AnonymousThreadClass {
public static void main(String[] args) {
//Anonymous class with constructor argument
Thread t = new Thread(new Runnable() {
public void run() {
System.out.println("Child!");
}
});
t.start();
System.out.println("Parent!");
}
}

Output:

Parent!

Child!

 

Difference Between Regular and Anonymous Inner Class

 

In this article, we saw what is an anonymous inner class and how can we create one. we went through the syntax of anonymous inner class and also how we can create an anonymous class in 3 ways and with this, we come to an end of this Anonymous Class in Java article. Check out the Java training by Edureka.

Got a Question? Mention it in the Comment Section of this article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES