Anonymous inner class is a type of inner class which is declared without a name.
Basic Syntax for Anonymous Inner Class:
/* Give importance to the beginning curly braces & there is a fact that the semicolon at the end is also important, While creating an anonymous class:*/
AnonymousInnerClass karthikh= new AnonymousInnerClass ()
{
// Example code comes under here…
};
- Many: Java interview Q&A available here
Basic Syntax for Argument Defined Anonymous Inner Class
class InterfaceImpl implements SampleInt
{
void interfaceFunction()
{
//Give some clear cut explanations about your code here.
}
}
Consider the following code as an example for anonymous inner class:
interface SampleInt
{
void InterfaceFunction();
}
class SampleClass
{
void SampleFunction(SampleInt s)
{}
}
classDifferentClass
{
void start()
{
SampleClass sa=new SampleClass();
sa.SampleFunction(You can do anything here?!);
}
}
The SampleInt present in the above code has only one method named as “interfaceFunction”. The class named as “SampleClass” has a method named “SampleFunction” which accepts an argument from “SampleInt”. If the “SampleFunction” called from another object, the implementation of “SampleFunction” is done by creating a new separate class. The newly created separate class will implement the interface. Using the implemented interface a new object can be created. If the created object is passed through the “SampleFunction”. Our example program will resemble the following code:
interface SampleInt
{
void InterfaceFunction();
}
class SampleClass
{
void SampleFunction(SampleInt s)
{}
}
classDifferentClass
{
void start()
{
SampleClass sa= new SampleClass();
InterfaceImpl imp = new InterfaceImpl();
sa.SampleFunction(imp);
}
}
Instead of following this complicated process, we can directly define anonymous inner class inside an argument. When we call an anonymous inner class inside an argument, our example program will look like the code given below.
interface SampleInt
{
void interfaceFunction();
}
class SampleClass {
void SampleFunction(SampleInt s) {}
}
classDifferentClass
{
void start()
{
SampleClass sa =new SampleClass();
sa.SampleFunction(new SampleInt())
{
public void interfaceFunction()
{
System.out.println(“Wow!! My Text is here!”);
} //An end of an interfaceFunction
}//An end of an anonymous inner class definition
);
}
}
In the program given above, an instance of a class is created to implement an interface named as “SampleInt”. The interface is not directly instantiated but an anonymous class is defined to interface the “SampleInt”. After the instantiation of interface, an instance of anonymous inner class is created. The bolded lines in the above program represent the syntax of argument defined anonymous inner class.
Argument defined anonymous inner classes are used when there is no object in class which can implement an interface. So a class is required to implement an interface which can create an instance of a method. Consider the following program as an example:
class MyToughClass
{
void do()
{
ClassOne A= new ClassOne();
A.doGoodthing(new InterfaceOne()
{
public void doGoodthingElse()
{
System.out.println(“Hollaahh!!!”);
}
});
}
}
interface InterfaceOne{
void doGoodthingElse();
}
class ClassOne{
void doGoodthing(InterfaceOne B) { }
}
In the program “doGoodthing” is called to object of ClassOne but the instance of method is taken from Interface1. So implementation of class and instance of the class are passed into the argument doGoodthing(). To create a definition for anonymous class which implement an interface, the syntax of argument defined anonymous inner class is used.Only argument defined anonymous inner class end with curly brace and parenthesis while other anonymous inner classes have only curly braces. Argument declaration inside an anonymous inner class is final and no other operations are allowed.