1. What is an anonymous inner class?
An inner class, a class that exists inside another class, and an anonymous inner class, as the name implies, is an inner class without a name.
2. Why do I need anonymous internal classes?
Each inner class is capable of inheriting an implementation class (Implementation). Therefore, inner class is not limited by whether outer class has been inherited from an implementation class.
Some problems with design and programming can become tricky if the ability of inner class to inherit from multiple representational (concrete) or abstract classes is reduced.
So, looking at inner class from a point of view, you can say it's a complete solution to multiple inheritance problems. Interface can solve some of these problems, but inner classes can effectively and realistically allow multiple implementations to inherit (multiple implementation). In other words, inner classes actually allows you to inherit multiple non-interface. Using an inner class from this level is typically instantiated and initialized by its parent or inherited interface, as shown in 3, when the name of the inner class is superfluous, so The anonymous inner class is used
3, how to implement anonymous internal class?
The implementation of anonymous inner class is very simple, such as an interface
Interface A{
public void method();
}
At this time, you can write the following statement to get the object of the implementation class of interface a
A a=new A(){ public void method(){System.out.println("hehe");} };
And the {public void method() {system. Out. Println ("he");}}; is to define an anonymous inner class
4. Answers to questions
The above can be seen in the anonymous inner class must be implemented with a class or an interface, if at this level it can inherit other classes can also implement the interface,
But it is impossible to pass the extends or implements keyword.
Java face question Anonymous Inner class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?