It is possible to implement an interface or extend the class of anonymous inner class. The interfaces of anonymous inner classes are always static. An anonymous inner class can either implement an interface or extend the class but it cannot do both at the same time.
Example for creating interface in anonymous inner class
In the program given below, an interface with the name “World” is created. Inside the interface, a class named “Matrix” and a method are declared. Inside the “Matrix” class, an instance of anonymous inner class which implements the interface of “World” is created. The instantiated class is declared anonymous because it has no name.
interface MyWld
{
public void open();
}
class M1
{
MyWld w= new MyWld()
{
public void open()
{
System.out.println(“This interface is MyWld class”);
}
};
In Java programming, it is common to witness classes implementing interfaces using the keyword “implements”. Only in anonymous inner class, an instance of interfaced class is created without the use of keyword “implements”. It is impossible to implement or interface a class which does not have a name. Anonymous inner classes are given a temporary name during interfacing. Instantiate of anonymous inner class can only be used during the declaration of interface.
Comparison of interface between anonymous inner classes and other classes.
Let us consider the program given below for example
interface NewTest
{
public void S1();
public void S2();
}
class NewTestImp implements NewTest
{
public void S1()
{
System.out.println(“Details of S1()”);
}
public void S2()
{
System.out.println(“Data of S2()”);
}
}
public class AnonymousInsideDemo
{
public static void main(String args[])
{
NewTestImp NewtestImp=new NewTestImp();
NewtestImp.S1();
new NewTestImp().S1();// This an anonymous Object creation
}
}
In the above program, the interface “NewTest” is declared. The declared interface “NewTest” is implemented using class objects S() and S2(). To implement the class objects, a class named “Imp” is created. Using anonymous inner class the creation of “Impl” class can be avoided.
The program given below uses anonymous inner class to implement an interface.
interface NewTest
{
public void S1();
public void S2();
}
public class AnonymousInsideDemo
{
public static void main(String args[])
{
new NewTest() //Method
{
public void S1()
{
System.out.println(“Print : S1()”);
}
public void S2()
{
System.out.println(“Collection of S2()”);
}
}.S1(); //Object creation
}
}
In the program given above, a class will be created internally which will implement the interface “NewTest”. Only one method can be implemented using anonymous inner class because the internal class and objects created for implementing interface will be lost immediately so it is not possible to call the second method using the same object.
- Many: Java interview Q&A available here
Important points to remember while creating anonymous inner class:
- Anonymous inner classes cannot have a constructor because it’s parameters cannot be passed while instantiation.
- All the variables including local variables can be accessed using anonymous inner classes.
- Methods of class can be accessed by anonymous inner class.