Java - Interface, instantiating an interface?

前端 未结 3 468
广开言路
广开言路 2020-12-11 05:50

So I just found this code example online a while ago and I\'m going over it again but quite confused.

From looking at it, what I gather (and it might be wrong) is t

相关标签:
3条回答
  • 2020-12-11 06:24

    This is called an anonymous inner class.

    It creates an un-named class that implements the Printer interface.

    0 讨论(0)
  • 2020-12-11 06:48

    You need a Printer object for the print function of NumberPrinter. When you call that function you don't actually instantiate the Printer interface but you instantiate an implementation of it and this is why it's working.

    Your assumption was correct by the way.

    0 讨论(0)
  • 2020-12-11 06:51

    Your assumption is correct, and you cannot instantiate an interface. You can instantiate an anonymous class however, which is what the code is doing.

    0 讨论(0)
提交回复
热议问题