Introduction to Anonymous Inner Class in Java
Anonymous Inner Class in Java is an inner class or nested class. Inner class is a class which is present inside an outer class. So an anonymous inner class is an inner class that has no name. It is either a subclass of a class or an implementation of an interface. So if we have to override method of class or interface we can use anonymous inner class. In case of the anonymous inner class a curly brace is followed by the semicolon.
There are two ways to create an anonymous inner class in java.
- Class
- Interface
Syntax:
Below is the syntax, where class can be abstract class or concrete class or interface.
class t = new class()
{
// class variables and methods members
public void class_method()
{
/* some code of the class_method */
}
};
Examples of Anonymous Inner Class in Java
Given below are the examples mentioned:
Example #1
Example where we create an interface and simple class which implement that interface.
Code:
package p1;
interfaceEmp
{
inteid = 2101;
voidgeteid();
}
// Implclass implement an Emp Interface and provide the defination for geteid() method
classImplClassimplementsEmp
{
@Override
publicvoidgeteid()
{
// display the eid
System.out.print("Employee id is "+eid);
}
}
class Demo
{
publicstaticvoidmain( String[] arg )
{
// create object of Implclass which implements Emp interface
ImplClassob=newImplClass();
// call geteid() method which implemented by Implclass
ob.geteid();
}
}
4.8 (8,018 ratings)
View Course
Output:
As in above code an interface Emp is created with geteid() method and eid=2101. ImplClass implements an Emp Interface and provide the defination for geteid() method. There is no need to write a separate class ImplClass, instead can be use anonymous inner class.
Example #2
Here we rewrite the above java code to see an inner class working. The ImplClass class is accessible to other classes in the application. However, the functionality define in the ImplClass class in not required by the other class in the application. Therefore we need not define an outer class. In addition instance of this class is used only once. Therefore, with the help of an anonymous inner class, the functionality provided by ImplClass class can be implemented. In below example we created object of the class directly by implementing the Empinterfece.
Code:
package p1;
interfaceEmp
{
inteid = 2101;
voidgeteid();
}
class Demo
{
publicstaticvoidmain( String[] arg )
{
// Implclass is hidden inner class implement an Emp Interface
// Implclass class name is not created but an object is creating below
Empob = newEmp() {
@Override
publicvoidgeteid() {
// display the eid
System.out.print("Employee id is "+eid);
}
};
// call geteid() method which implemented by Implclass
ob.geteid();
}
}
Output:
As in above code an object of Emp is not created, implicitly an object of ImplClass(may not be same name) class is created. Note that inner class has no name, so compiler decide a name and creates it which implements the Emp interface.
Example #3
Java code to create an anonymous Inner class that extends a class.
Code:
package p1;
classEmp
{
voiddispMesg()
{
System.out.println("This message is from main class");
}
}
class Demo
{
publicstaticvoidmain( String[] arg )
{
// Anonymous hidden inner class extends an Emp class
// Anonymous class name is not created but an object is creating below
Empob = newEmp() {
@Override
// override dispMesg() method in child Anonymous inner class
publicvoiddispMesg() {
System.out.print("This message is from child class");
}
};
// call geteid() method which implemented by Implclass
ob.dispMesg();
}
}
Output:
Example #4
Java code to create an anonymous Inner class that extends an abstract class.
Code:
package p1;
abstractclassEmp
{
voiddispMesg()
{
System.out.println("This message is from main class");
}
abstractvoidabstrct_method();
}
class Demo
{
publicstaticvoidmain( String[] arg )
{
// Anonymous hidden inner class extends an Emp abstract class
// Anonymous class name is not created but an object is creating below
Empob = newEmp() {
@Override
publicvoiddispMesg() {
System.out.println("This message is from child class");
}
@Override
voidabstrct_method()
{
System.out.println("Abstract Method");
}
};
ob.dispMesg();
ob.abstrct_method();
}
}
Output:
Example #5
Java code to create an anonymous Inner class that defines inside constructor or method argument where we will use the predefined Thread class and runnable interface to create and run thread.
Code:
package p1;
class Demo
{
publicstaticvoidmain( String[] arg )
{
// Anonymous hidden inner class extends an Emp class
// Anonymous class define constructor argument
Thread obt = newThread(new Runnable()
{
@Override
publicvoidrun()
{
System.out.println("Runing child Thread.");
}
});
System.out.println("Runing main Thread.");
obt.start();
System.out.println("Runing main Thread.");
}
}
Output:
Conclusion
An anonymous inner class is an inner class that has no class name. There are different ways to create an anonymous inner class like by extending the concrete class, by extending the abstract class and by implementing an interface.
Recommended Articles
This is a guide to Anonymous Inner Class in Java. Here we discuss the introduction to anonymous inner class with respective examples for better understanding. You may also have a look at the following articles to learn more –