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

前端 未结 12 1217
轻奢々
轻奢々 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 20:03

    You do this to make sure later when working with the variable you (or anyone using your classes) won't rely on methods specific for the implementation chosen (ArrayList, HashMap, etc.)

提交回复
热议问题