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
  • 125,519
  • 44
  • 317
  • 399
optimistic_creeper
  • 2,661
  • 3
  • 22
  • 35
  • 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