I have a class (EntireFile) that extends the LinkedHashMap. I try to cast:
EntireFile old = (EntireFile) functionReturningLinkedHashMap();
It throws exception with message: “java.util.LinkedHashMap cannot be cast to com.gmail.maximsmol.YAML.GroupMap”.
public class EntireFile extends LinkedHashMap<String, GroupMap>
public class GroupMap extends LinkedHashMap<String, CategoryMap>
public class CategoryMap extends LinkedHashMap<String, LinkedHashMap<String, Integer>>
Please help me to solve the error!
The problem is that the reference returned simply isn’t a reference to an instance of
EntireFile. IffunctionReturningLinkedHashMapjust returns aLinkedHashMap, it can’t be cast to anEntireFile, because it isn’t one – how would it get any extra information about it?(Judging by your exception, you’re actually talking about
GroupMaprather thanEntireFile, but the same thing applies.)There’s nothing special about
LinkedHashMaphere – the same is always true in Java: