There is such code:

abstract class AClass{ public void abc() { System.out.println("Wow!"); } } public class Demo{ public static void main(String[] args){ AClass tmp = new AClass() {}; tmp.abc(); } } 

and it works. Nowhere did I find the information into what exactly the curly brackets turn this object into.

How else, except as an object of the abstract class AClass , can the tmp object be interpreted?

  • one
    This construct creates an object of an anonymous class, which is implicitly inherited from your abstract class. - Serodv
  • one
    usually in an abstract class, methods are defined whose logic needs to be defined in the heirs. You do not have these, because the braces are empty. - Riĥard Brugekĥaim
  • 2
    If you make Sytem.out.println (tmp.getClass ()), you will see that you have a different class. - aleshka-batman
  • abstract class cannot have objects. - Roman C

1 answer 1

This is a construct, and it is called an " anonymous class ":

 AClass tmp = new AClass() {}; 

It turns into a normal. If you compile it and see what files it turned out, you will find another file with approximately the same name - <Имя внешнего класса>$<Числовой суффикс>.class .

Predict the exact name you can not, so call him a constructor or something else does not work. A single instance of this class will be assigned to the variable tmp .

Interpret tmp as an AClass heir AClass