Generics in interfaces parameters

My post is a feature request. If anyone have informations on forthcoming and related developments, please send me.

I want to implement a graph class with nodes and edges collections and express in the implements section that this class supports such features (I don't take into account any delegation mechanism), graph can be considered as a String / Node map or as String / Edge one :

public class Graph implements Map<String, Node>, Map<String, Edge> {

...

}

but when compiling, I have interfaces method conflicts such as :

"The return type is incompatible with Map<String,Edge>.get(Object), Map<String,Node>.get(Object)"

because one get(Object) method is present in each interface. I would have rather liked to have a get(Node) and a get(Edge) methods. In other words, I would have liked to have Map<K,V>.get(K) method in Map interface.

Does anyone know why Java Generics are not fully introduced in interfaces and maybe in some classes ? Is it a technical reason ? A workaround ? An intermediate development stage ?

Thx.

[1117 byte] By [Etienne2072] at [2007-9-30 22:04:44]
# 1

> public class Graph implements Map<String, Node>, Map<String, Edge> {

The problem is that after compilation, when the object code is produced, any "type parameter" information is erased (you need to read up on Java reflection more closely).

Thus, there is only one Map class in the runtime (not one for every possible combination of type parameters). and any references to, say, Map<String,Node>.put() become, at runtime, just Map.put().

So you can't do what you want, because at compile time, your two parent interfaces are "different", but at runtime, they would be the same. The message could be better, I agree.

To do what you want:

* If your two classes (Edge and Node) have no relationship to each other, then you'll have to implement Map<String,Object>.

* If they do have a common ancestor, you can declare it as extending Map<String,CommonAncestor>.

In both cases, you'll have to, e.g. downcast the result of get() to the right type.

shankar.unni at 2007-7-7 11:17:51 > top of Java-index,Administration Tools,Sun Connection...
# 2

> public class Graph implements Map<String, Node>,

> Map<String, Edge> {

> ...

> }

I would like to congratulate you personally on discovering the most marvellously obtuse and ambiguous usage of generics I have seen to date.

I'm sure there's worse to come, but this one raised a laugh from the well-developed "Generics agnostic" side of me.

No, it's not you, it's the fact that the syntax could even allow this vile construction.

/k1

komone at 2007-7-7 11:17:51 > top of Java-index,Administration Tools,Sun Connection...