What is an Anonymous Class?
In java, we can create a class inside another class called a nested class, and we can create a class with no name,i.e called an Anonymous Class.
In simple terms, a nested class with no name and only one object is created, said as Anonymous Class.
Note: It must be defined in another class.
Looking at the diagram, we see that anonymous classes along with local and nonstatic member ones form the so-called inner classes. Together with static member classes, they form the nested classes.
In the anonymous class we can declare the following:
- Fields
- Instance Initializers
- Local classes
- Extra methods
Syntax:
// AnonymousClass = interface,abstract/concrete class. AnonymousClass c = new AnonymousClass() class TUFOuterClass { // defining anonymous class object1 = new Type(parameterList) { // body of the anonymous class }; }
Note: Anonymous classes are defined inside an expression. So, the semicolon is used at the end of anonymous classes to indicate the end of the expression.
Example of Anonymous Class:
Code:
Java Code
abstract class CheckClass {
abstract void check();
}
class TUF {
public static void main(String args[]) {
// defining anonymous class
CheckClass p = new CheckClass() {
// body of anonymous class
void check() {
System.out.println("Hello there Anonymous here!");
}
};
p.check();
}
}
Output: Hello there Anonymous here!
Ways to create Anonymous Class:
- Class(can also be Abstract)
- Interface
By Extending Class:
Code:
Java Code
//Program to illustrate Anonymous Inner class by extending other class
class Tuf {
public void display() {
System.out.println("Inside the TUF class");
}
}
class AnonymousDemo {
public void createClass() {
// creation of anonymous class extending class Tuf
Tuf p1 = new Tuf() {
public void display() {
System.out.println("Inside an anonymous class.");
}
};
p1.display();
}
}
class TestAnnonymousInner {
public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
}
}
Output: Inside an anonymous class
By Implementing Interface:
Code:
Java Code
//Program to illustrate Anonymous Inner class by implementing interface
class TUF {
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!
Difference Between Regular and Anonymous Inner Class
- A general class can implement any no. Of interfaces at a time. Anonymous inner class can implement only one interface at a time.
- With anonymous we cannot write a constructor because the anonymous inner class does not have a name and the name of the constructor should be the same as the class name.
Important point:
- Anonymous class has no name
- It can be instantiated only once
- It is usually declared inside a method or a code block, with curly braces ending with a semicolon.
- It is accessible only at the point where it is defined.
- It does not have a constructor simply because it does not have a name
- It cannot be static
Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article