Anonymous classes.

An anonymous class is a local class with no name. You can declare an anonymous (unnamed) class that can extend (extends) another class or implement (implements) an interface. The declaration of such a class is executed simultaneously with the creation of its object using the new operator.

Anonymous classes are effectively used, as a rule, to implement (override) several methods and create your own methods of an object. Also, when a local class is used only once, you can use the syntax of an anonymous class, which allows you to combine the definition and use of the class.

Constructors in anonymous classes cannot be defined or overridden. An anonymous class cannot have constructors because the name of the constructor must be the same as the name of the class, and in this case the class has no name.

Since the anonymous class does not have a name, then, as I said, its constructor cannot be defined in the body of the class. This is one of the main limitations of anonymous classes. Any arguments you specify in parentheses following the parent class name in the anonymous class definition are implicitly passed to the parent class’s constructor. Most often, anonymous classes are used to extend parent classes with simple classes that do not require constructor arguments, so the parentheses in the definition of an anonymous class are often empty.

Anonymous classes can be nested within each other, which greatly confuses the code and makes these constructs incomprehensible, so these features are usually not used. Now let’s look at a simple example in practice:


In the example on the left, red boxes indicate code fragments where anonymous classes are declared. The first one implements the Iout interface, the second extends the External class. Notice the colon after the curly braces that close the class declaration, it is required, since essentially an anonymous class declaration is an expression. This means that it can be written as part of a larger expression, such as a method call. I’ve provided fairly simple examples, although the syntax may still seem complex. And I started by declaring an anonymous class that implements the Iout interface, since in this example it is easier to understand why anonymous classes are called anonymous – since they have no name. As you remember, it is impossible to create an instance of the interface, since it is a completely abstract class, but here we create a class that implements the interface, but this class has no name and we assign a link to this anonymous class to the interface variable iout. I hope it’s simple? Who is not confused?

In the second example, not everything is so transparent, since it may seem that the class still has the name External, but this is not so. The class is still anonymous because it extends the External class and is not an instance of the External class, although the reference to this anonymous class is assigned to a variable of the External class, which is normal and normal. I hope that again no one is confused

Anonymous classes, if created as an extension of a super class, can refer to members of their super classes. The code and output of the program confirm this.