• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Class extending HashMap<K,v> is not HashMap?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If I have a class

why can't I use it in methods expecting Map<K,V>

e.g


I know the raw type of HashMap<String,String> is HashMap but EntityStatusMap is not a HashMap?

Thanks,

 
Peter Kovac
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the answer is that emptyMap returns a custom Map implementation.
 
Marshal
Posts: 76842
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have got the question the wrong way round. Your cast doesn’t work because the object returned is not an EntityStatusMap.
It might even not be a HashMap; you need to see the documentation, and you see its return type is plain simple Map. The cast fails at different stages, depending on whether the javac tool can determine that it will definitely fail at runtime or not.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another spin on saying the same thing as above:

This is a logical problem. You know that every EntityStatusMap is a HashMap and is a Map. But not every HashMap or every Map is a EntityStatusMap. So just because Collections.EmptyMap and Collections.emptyMap() are Maps (and maybe HashMaps) it doesn't mean they are EntityStatusMaps, and if they aren't EntityStatusMaps they can't be cast to one.

Reference casting doesn't do any conversions, it simply says 'I, the programmer, know this is an EntityStatusMap so you can assign it to an EntityStatusMap instance.' When you are wrong with that statement you get the exceptions/errors you see.
 
Campbell Ritchie
Marshal
Posts: 76842
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another spin is a useful things; if he didn’t understand the way I put it, he might understand somebody else’s explanation.
 
I don't always make ads but when I do they're tiny
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic