It is defining a class with no identity, i.e. a class without a name. They are used whenever you need to override the methods of a class, abstract class or interface. Anonymous inner class provides you with the blueprint, outline or template of the class but does not define or declare the behavior of the class. In this topic, we are going to learn about Anonymous Class in Java.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Let us look at some of the key concepts before proceeding.
One of the very important properties in an object-oriented language is Abstraction. Abstraction is the property that provides the blueprint/ template of methods, i.e. just declaration without defining them.
Java provides two such abstraction levels such as
- Interface
- abstract
- The interface supports complete abstraction, and it does not allow to define any method in it.
- Abstract classes support partial abstraction where a mixture of both abstract and normal method can reside.
- A class extends the abstract class and implements an interface.
- As abstraction is clear to you, we will look into one example where we have used interface or abstract class to write the anonymous inner class.
Example of Anonymous Class in Java
Following are the examples of anonymous class in java:
Example #1
Anonymous class for defining the method of an interface.
Code:
interface First
{
void printing();
}
public class Test
{
public static void main(String[] args)
{
First obj = new First()
{
public void printing() {
System.out.println("I am an inner class defining interface method");
}
};
obj.printing();
}
}
登录后复制
Output:
Explanation:
- An interface is created naming First, and a method is declared as printing.
- Remember all the methods in an interface are public and abstract
- Next, we created a concrete class, and in the main method, we have to define the interface method by creating the interface object.
- We cannot create an object of an interface or abstract class. But here, we are creating a class with no name and defining the method of that interface.
- After creating an instance of an interface, we open the curly braces just like we do for class and define the printing function we have declared in the First interface.
- At the end of the curly braces we have given after instance creation, we must provide asemicolon.
- Such a block is known as the Anonymous inner class.
- After the block, the first name as obj is used to call the function defined inside the anonymous inner class.
- This is a situation where we are defining interfaces inside any method so-known as a local inner class.
Example #2
Anonymous class for defining the method of an abstract class.
Code:
abstract class AbstractClass {
abstract class AbstractClass {
public abstract void display();
}
public class Test {
public static void main(String[] args){ AbstractClass obj = new AbstractClass()
{
public void display() {
System.out.println("I am an inner class defining abstract class method.");
}
};
obj.display();
}
}
登录后复制
Output:
Explanation:
- An abstract class is created naming AbstractClass, and an abstract method is declared as a display.
- In an abstract class, the method named as abstract is declared a without abstract method can be defined.
- Next, we created a concrete class, and in the main method, we have to define the abstract class method by creating its object.
- After creating an abstract class instance, we open the curly braces just like we do for class and define the display function we have declared in AbstractClass.
- We do not have to write the abstract keyword in front of the display method again as we have already declared it abstract in AbstractClass.
- At the end of the curly braces we have given after instance creation, we must provide a semicolon.
- After the block, the instance created for AbstractClass name as obj is used to call the function defined inside the anonymous inner class.
- This is a situation where we are defining interfaces inside any method so-known as a local inner class.
How will the files be generated for the Anonymous class?
For the above Test class, a class file will be generated like normal named as Test.class.
For the anonymous inner class created, as there is no identity, a class file is still generated named as:
Test$1.class ---> anonymous class
登录后复制
If you create another anonymous inner class in the same class Test, then the class file will be named as:
Some differences from normal classes:
- Anonymous inner classes can implement only one interface at a time, whereas normal classes can implement manyinterfaces.
- A normal class extends an abstract class and implements an interface at one time, but the anonymous inner class can only extend an abstract class or implement an interface at a single.
- We cannot define any constructor inside the anonymous inner class due to a lack of class name/ identity.
- We cannot have any static variables in an anonymous inner class as an inner class are associated with an instance.
Usage
Anonymous inner classes are mostly used in Graphic User Interface applications. When an object created has to be used only once, then the anonymous class can be used. When an object created has to be used only once, then the anonymous class can be used.
We can create an instance of an inner class object by first creating an object of outer class such as:
Outer class as Person so for creating an instance of an inner class inside person class
Person p = new Person();
Person.InnerClass innerClassObj = p.new InnerClass();
登录后复制
Conclusion
Anonymous inner class is a very important feature to know the power of object-oriented language and also is frequently asked questions in the technical interviews. Anonymous inner class gives you the liberty to define a class body method with no name.
以上是Java 中的匿名类的详细内容。更多信息请关注PHP中文网其他相关文章!