variable of Interface type

后端 未结 3 1624
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 09:01

I am learning Java, I saw the following description regards to interface in a book:

When a variable is declared to be of an interface typ

3条回答
  •  孤城傲影
    2021-02-04 09:38

    You don't instantiate an interface. You create classes that implement the interface, instantiate those, but reference the interface (as opposed to the implementing class).

    This allows your code to remain implementation-neutral, similar to using a List reference, but an ArrayList implementation.

    When combined with dependency injection it makes testing and extensibility much easier, too.

提交回复
热议问题