You are reading the article Anonymous Inner Class In Java updated in September 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Anonymous Inner Class In Java
Introduction to Anonymous Inner Class in JavaAnonymous Inner Class in Java is an inner class or nested class. An inner class is a class that 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 a class or interface method, we can use an anonymous inner class. In the case of the anonymous inner class, a curly brace is followed by the semicolon.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Class
Interface
Syntax:
Below is the syntax, where the class can be an abstract class or a concrete class or interface.
class t = new class() { public void class_method() { /* some code of the class_method */ } }; Examples of Anonymous Inner Class in JavaGiven below are the examples mentioned:
Example #1An example where we create an interface and a simple class that implements that interface.
Code:
package p1; interface Emp { int eid = 2101; void geteid(); } class ImplClass implements Emp { @Override public void geteid() { System.out.print("Employee id is "+eid); } } class Demo { public static void main( String[] arg ) { ImplClass ob=new ImplClass(); ob.geteid(); } }Output:
As in the above code, an interface Emp is created with the geteid() method and eid=2101. ImplClass implements an Emp Interface and provides the definition for the geteid() method. There is no need to write a separate class ImplClass; instead, it can be use as an anonymous inner class.
Example #2Here 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 ImplClass class’s functionality is not required by the other class in the application. Therefore we need not define an outer class. In addition, an instance of this class is used only once. Therefore, with the help of an anonymous inner class, the ImplClass class’s functionality can be implemented. In the below example, we created the object of the class directly by implementing the Empinterfece.
Code:
package p1; interface Emp { int eid = 2101; void geteid(); } class Demo { public static void main( String[] arg ) { Emp ob = new Emp() { @Override public void geteid() { System.out.print("Employee id is "+eid); } }; ob.geteid(); } }As in the above code, an object of Emp is not created; implicitly, an object of ImplClass(may not be the same name) class is created. Note that the inner class has no name, so the compiler decides a name and creates it, implementing the Emp interface.
Example #3Java code to create an anonymous Inner class that extends a class.
Code:
package p1; class Emp { void dispMesg() { System.out.println("This message is from main class"); } } class Demo { public static void main( String[] arg ) { Emp ob = new Emp() { @Override public void dispMesg() { System.out.print("This message is from child class"); } }; ob.dispMesg(); } }Output:
Example #4Java code to create an anonymous Inner class that extends an abstract class.
package p1; abstract class Emp { void dispMesg() { System.out.println("This message is from main class"); } abstract void abstrct_method(); } class Demo { public static void main( String[] arg ) { Emp ob = new Emp() { @Override public void dispMesg() { System.out.println("This message is from child class"); } @Override void abstrct_method() { System.out.println("Abstract Method"); } }; ob.dispMesg(); ob.abstrct_method(); } }Output:
Example #5Java 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 { public static void main( String[] arg ) { Thread obt = new Thread(new Runnable() { @Override public void run() { System.out.println("Runing child Thread."); } }); System.out.println("Runing main Thread."); obt.start(); System.out.println("Runing main Thread."); } }Output:
ConclusionAn anonymous inner class is an inner class that has no class name. There are different ways to create an anonymous inner class, like extending the concrete class, extending the abstract class, and implementing an interface.
Recommended ArticlesThis 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 –
Java Identifiers
Java ItemListener
Java ServerSocket
Java 9 Features
You're reading Anonymous Inner Class In Java
Update the detailed information about Anonymous Inner Class In Java on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!