Look at the extreme case. IFace say IFace is an acutally Object , then your code looks like this:
static Map<String, Object> myMap = new HashMap<>(); public static void main(String[] args) throws Exception { myMap.put("ONE", 1); myMap.put("TWO", "TWO"); myMap.put("THREE", new Date()); final Calendar calendar1 = getThing("ONE"); final Calendar calendar2 = getThing("TWO"); final Calendar calendar3 = getThing("THREE"); } public static <T> T getThing(String key) { return (T) myMap.get(key); }
So you put in a class than extends Object in your Map (so any class ).
But, when you call getThing , you do an implicit cast to your desired type. It should be pretty obvious that I can call getThing with any class as well, and it will blindly try to apply to it.
In the above example, I put some things in my Map , and then try to get them all like Calendar s.
source share