![]() |
VOOZH | about |
Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as overriding methods of a class or interface, without having to actually subclass a class.
Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.
The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.
Syntax:
// Test can be interface,abstract/concrete class
Test t = new Test()
{
// data members and methods
public void test_method()
{
........
........
}
};
Now let us do discuss the difference between regular class(normal classes) and Anonymous Inner class
Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the Anonymous Class
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
Anonymous classes also have the same restrictions as local classes with respect to their members:
Note: We can declare the following in anonymous classes as follows:
- Fields
- Extra methods (even if they do not implement any methods of the supertype)
- Instance initializers
- Local classes
Ways:
Anonymous inner classes are generic created via below listed two ways as follows:
Now let us take an example with which we will understand anonymous inner class, let us take a simple program
Example
Output:
👁 ImageOutput explanation:
In the above program, interface Age is created with getAge() method and x=21. Myclass is written as an implementation class of Age interface. As done in Program, there is no need to write a separate class Myclass. Instead, directly copy the code of Myclass into this parameter, as shown here:
Age oj1 = new Age()
{
@Override
public void getAge()
{
System.out.print("Age is " + x);
}
};
Here, an object to Age is not created but an object of Myclass is created and copied in the entire class code as shown above. This is possible only with anonymous inner class. Such a class is called ‘anonymous inner class’, so here we call ‘Myclass’ as anonymous inner class.
Example:
Age is 21
Based on declaration and behavior, there are 3 types of anonymous Inner classes:
Type 1: Anonymous Inner class that extends a class
We can have an anonymous inner class that extends a class. For example, we know that we can create a thread by extending a Thread class. Suppose we need an immediate thread but we don't want to create a class that extends Thread class all the time. With the help of this type of Anonymous Inner class, we can define a ready thread.
Example
Main Thread Child Thread
Type 2: Anonymous Inner class that implements an interface
We can also have an anonymous inner class that implements an interface. For example, we also know that by implementing Runnable interface we can create a Thread. Here we use an anonymous Inner class that implements an interface.
Example
Main Thread Child Thread
Type 3: Anonymous Inner class that defines inside method/constructor argument
Anonymous inner classes in method/constructor arguments are often used in graphical user interface (GUI) applications. To get you familiar with syntax lets have a look at the following program that creates a thread using this type of Anonymous Inner class
Example
Main Thread Child Thread
However, constructors can not be declared in an anonymous class.