Polymorphism and Interfaces in Java (can polymorphism be used to implement interfaces…why?)

后端 未结 8 2023
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 17:14

In the real world what do people use this for (to solve what types of problems)? Can I see some example code of these working together? All I can find is code about cats and

8条回答
  •  囚心锁ツ
    2020-12-05 17:35

    Map peopleByName = new HashMap();
    

    If, down the road, I decide the memory overhead of HashMap is too much, I can re-do this as a TreeMap, and live with the slightly more expensive lookup times

    Map peopleByName = new TreeMap();
    

    Because peopleByName is a Map, not a TreeMap or a HashMap, all my calls are guaranteed to work on either map regardless of implementation.

    This is best illustrated with the following example

    public class CatsAndDogsDrinkingMilkAndCoffee {
    
       // what, no? :-(
    
    }
    

提交回复
热议问题