-1
Interface obj = new Interface(){}; --> interface reference

we know interface can't be initialized but However the anonymous class implementation makes above obj just like object of any class and can do all things that every normal object does.

If I'm wrong, how anonymous class implementation is different from object initialization?

  • 2
    I'm not exactly sure what you're asking. An anonymous inner class is, as the name suggests, a class, so why do you think an instance of this class should behave differently from other object instances? – Jeroen Steenbeeke Nov 16 '20 at 10:02

1 Answers1

0

Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time. Whereas in Object initialisation, the class is already declared with some name and only an object is created during object initialisation. There is no difference function wise. it is just that anonymous class is declared and used only for that particular instance whereas a normal class can be reused n number of times.

sachin
  • 777
  • 5
  • 10