Why do we first declare subtypes as their supertype before we instantiate them?

前端 未结 12 1219
轻奢々
轻奢々 2021-02-01 19:27

Reading other people\'s code, I\'ve seen a lot of:

List ints = new ArrayList();
Map map = new HashMap();
12条回答
  •  梦谈多话
    2021-02-01 19:51

    Using interfaces has the main advantage that you can later change the implementation (the class) without the need to change more than the single line where you create the instance and do the assignment.

提交回复
热议问题