Anonymous inner class is an inner class without a name.
Anonymous class is commonly used to create implementation classes of listener interfaces. Anonymous class is very useful while creating an instance of subclass(of a class) or implementing an interface without actually extending and with less code.
The main purpose of creating anonymous inner class is to override one or more methods of the super class or to implement methods of an interface with creating a just-in–time and anonymous instance.
There are two ways to create anonymous inner class:
- As a subclass of class.
- As an implementer of an interface.
1. Anonymous inner class type 1 – instance of Class
Let us first write some code to understand this type of inner class.
class Parent { public void print() { System.out.println("this message is from parent class"); } } class MyClass { Parent p = new Parent() { public void print() { System.out.println("this message is from anonymous class"); } }; }
1.1 Explanation
- Parent is a class. This class has a method
print()
which does nothing specific and just prints a message. MyClass
is another class which has a member variablep
which is of typeParent
.- As you can notice there is no semicolon(;) after
new Parent()
but a curly brace ({). This is syntax of writing an anonymous class. - This code is creating a just-in-time, anonymous, new instance of subclass of
Parent
class. - There is a semicolon(;) after the closing curly brace (}). This is important – Anonymous inner class’ code ends with a semicolon.
Note: An anonymous, just-in-time, new instance is created of the subclass in this case. Since the new instance is of subclass, polymorphism comes into picture. That is, only those methods can be called on the anonymous inner class reference which are defined in parent class.
1.2 Example
package com.learnitweb; class Parent { public void print() { System.out.println("this message is from parent class"); } } class MyClass { Parent p = new Parent() { public void print() { System.out.println("this message is from anonymous class"); } }; public void executeTest() { p.print(); } public static void main(String args[]) { MyClass myclass = new MyClass(); myclass.executeTest(); } }
Output
this message is from anonymous class
2. Anonymous inner class type 2 – implementer of interface
In this type of anonymous inner class, an anonymous implementer of the interface type is created.
interface Printable { public abstract void print(); } class MyClass { Printable p = new Printable() { public void print() { System.out.println("this message is from anonymous class"); } }; }
2.1 Explanation
Printable
is an interface. This interface has an abstract methodprint()
.MyClass
is a class which has a member variablep
which is of typePrintable
.- As you can notice there is no semicolon(;) after
new Printable()
but a curly brace ({). This is syntax of writing an anonymous class. - This code is creating just-in-time, anonymous, a new implementer of
Printable
interface. - There is a semi-colon(;) after the closing curly brace (}). This is important – Anonymous inner class’ code ends with a semicolon.
2.2 Example
package com.learnitweb; interface Printable { public abstract void print(); } class MyClass { Printable p = new Printable() { public void print() { System.out.println("this message is from anonymous class"); } }; public void executeTest() { p.print(); } public static void main(String args[]) { MyClass myclass = new MyClass(); myclass.executeTest(); } }
Output
this message is from anonymous class
2.3. Important points
- You can not instantiate an interface. While creating an anonymous inner class, you can see use of
new
with an interface that isnew Printable(){
in the above example. This is only possible only in this case. Otherwise you can not usenew
with the interface. - The implementer in this case can implement only one interface at a time.
3. Define an anonymous inner class as an argument
Yes, you can define an anonymous inner class as an argument. Let us see this with an example.
package com.learnitweb; interface Printable { public abstract void print(); } class Printer { public void print(Printable p) { p.print(); } } class MyClass { public static void main(String args[]) { Printer printer = new Printer(); printer.print(new Printable() { public void print() { System.out.println("Message from anonymous class"); } }); } }
Output
Message from anonymous class
3.1 Explanation
Printable
is an interface which has an abstract methodprint()
.Printer
is a class which has method print which accepts argument of typePrintable
.Myclass
has a method main. In this method,printer
is an instance of typePrinter
. While callingprint()
method, we need to pass an argument of typePrintable
. So we have created an anonymous implementer of typePrintable
. Note that we have created this implementer in the argument itself while calling the method.
4. Important point
- The argument local inner class ends with });
- The plain anonymous inner class ends with };