Thursday, 29 August 2013

Why a Map with superclass values can't accept subclass objects?

Why a Map with superclass values can't accept subclass objects?

I have two classes: superclass Cluster and subclass XCluster. XCluster
extends Cluster.
I have this method signature:
public Map<Integer, XCluster> getClusters() {
...
}
I have this variable:
Map<Integer, Cluster> clusters = getClusters();
I can't compile this. Eclipse says that the method getCluster() doesn't
return this kind of type. That I need to change the type from XCluster to
Cluster.
What's wrong?

No comments:

Post a Comment