An Anonymous inner class is declared by without name and the syntax of an anonymous inner class is also different from other inner classes. Many Java developers believe that anonymous inner classes increase the complexity of the code. But they are extremely helpful in defining the subclass. It is easy to define callbacks using anonymous inner classes. Behaviour objects like names and threads can be easily added using anonymous inner classes.
- Many: Java interview Q&A available here
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:*/
Matrix1 aInsta=new Matrix1()
{
// Put your source code here!
};
The sample code for anonymous inner class:
class Ulagam
{
public void read()
{
System.out.println(“Ahh!! Ulagam”);
}
}
class Matrix1
{
/* This Method will create a different inner class: */
Ulagam aInsta= new Ulagam()
{
public void read()
{
System.out.println(“Different Ulagam”);
}
};
}
There are two classes present in the program each classes are named as Ulagam and Matrix1. The behaviour of Ulagam class is mellow and it contains only one method named read(). The read() method prints a text “Ahh! Ulagam” right after calling. Inside the class “Matrix1”, an instance of world class is called using aInsta which in turn creates an anonymous class of “Ulagam”.
Is Anonymous Inner Class is a Sub Class?
In the example code used above, an anonymous inner class is created inside a curly brace which clearly explains that anonymous inner class is a sub class. The read() method present inside the Matrix 1class confirms anonymous inner class is a subclass because presence of method is not normal while creating class instance.
Why it is called as Anonymous Inner Class?
This class is called as anonymous because it does not have any name. Anonymous class don’t have name but it can create instance and has variables just like any other class. One easy way to differentiate anonymous inner classes from other classes is to identify the syntax difference. Anonymous inner classes are created using curly braces while other classes use parenthesis.
Polymorphism in Anonymous Inner Classes:
Polymorphism works well in anonymous inner classes. In our example program aInsta is a reference type of super class which will refer to a subclass. According to the example, code aInsta is a reference type of the class “Ulagam” and this process is called as polymorphism.
Let us Consider the following code as an example:
class Pets
{
void go()
{
}
}
class Dog1 extends Pets
{
void LoudBark()
{
}
}
Create an Instance for Pets class which points towards the class “Dog1”. The class “Dog1” was derived from the class “Pets”.
class PetsTesting
{
public static void main(String[] args)
{
Pets d1=new Dog1();
/* In this case, all legal, calling
the function ‘go’ is not at all a problem.
Why because is, it defined inside
the Pets class: */
d.go();
/* The Compiler Error is here! Calling
the Function LoudBark results
in an error. Why because is,
It is not defined in the Pets
class itself: */
d.LoudBark();
}
}
Calling the method d.go() in the above code is correct while calling method d.LoudBarkark() will result in compiler error. d.LoudBark() method was not declared in the “Pets” class. Even though the reference variable of “Pets” class refer to the subclass, it is not aware of the methods defined in the subclass.
Interface in Anonymous inner classes:
In the program given below, an anonymous inner class is used to implement an Apple interface and instance during the same time. Only anonymous inner class can define an interface without having the name of a class.
package com.javadadei.core;
public interface AppleInterface {
public void TalkMe();
}
package com.javadadei.core;
public class Creation{
/*
* Here we implemented an another different inner class that creates
* Apple Interface & also implemented an instance for the class as well.
*/
NewAppleInterface MyAppleInterface = new NewAppleInterface() {
public void TalkMe()
{
// Method description;
}
};
}
Points to remember while creating Anonymous inner classes
- Anonymous inner classes don’t have a name.
- Using Anonymous inner classes, a programmer can declare and instantiate a class at the same time.
- Normal classes in java are declared while anonymous inner classes are created as expressions.
- Access to members of the outer class is provided by anonymous inner class.
- Callbacks defined using anonymous inner classes, do not require enumerations, visitors and iterators.