The anonymous inner class has no name, so the anonymous inner class also creates its object while declaring the class. The declaration of an anonymous inner class is either inherited or based on an implementation interface.
In the anonymous inner class class, you can override the parent class's methods, or provide your own new methods and members. Note, however, that because anonymous inner classes do not have a name, there is no way to declare a reference to an anonymous inner class type, so the new methods and members provided can only be used internally and cannot be used outside.
The use of anonymous inner class objects is generally done through polymorphism, with references to the direct or indirect parent class that it inherits from to the object. Therefore, only its overridden parent class method can be called by reference, and its own methods and members cannot be called.
In an anonymous inner class based on an interface implementation, because it has no name and cannot be inherited, the solid cannot be abstract, so all the methods in the interface must be implemented in the class body.
Non-static statement blocks are required to initialize the member variables of the parent class in an anonymous inner class.
The role of anonymous inner classes is reflected in:
In certain cases, anonymous inner classes can reduce redundant code. Because in real-world development, there are many classes that are written by themselves that need to be used only once, and do not need to create objects more than once, using an anonymous inner class to synchronize the class code with the created object.