0

The code snippet below makes me puzzled:

HashMap<String, String> m  = new HashMap<>();
Map m1 = new HashMap<>();

System.out.println("m instanceof Map: "+(m instanceof Date)); //First
System.out.println("m1 instanceof Map: "+(m1 instanceof Date)); //Second

First gives a compilation error while the Second doesn't. Why? Can anyone help me explaining this, pls?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
  • 2
    I could create a `Map` implementation that is a subclass of `Date`. – Sotirios Delimanolis Jun 01 '16 at 13:55
  • Map is an Interface whereas HashMap is a class. So the Compiler can detect at compiletime, that HashMap can never be a Date, but a Map could be a Date (e.g. if you would create a subclass of Date, which implements Map). – Thomas Philipp Jun 01 '16 at 14:00

0 Answers0