7

I am implementing the Map<V,K> and the Collection<V> interface in one class, but the remove(Object) method occurs in both interfaces, therfore eclipse shows me some errors. The return types are different, one returns boolean and the other V but that doesn't seem to matter.

Is there some way of telling java/eclipse which method is actually being overridden?

EDIT: I have got an interface that all values must implement, it supplies the value with a getKey() method, making it possible to write an add function for the map. But there seems to be no way to let this one class look as a map and a collection at the same time?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
  • You might want to say what you're intending to do. A `Map` is fundamentally a key-value dictionary. A `Collection` is just a bunch of things. They are not conceptually the same thing at all. Do you mean that you want a `Map` whose *keys* can be accessed as a `Collection`? Because `keySet()` does that for example. – Sean Owen Oct 19 '11 at 09:26

5 Answers5

8

No, there is no a direct way.

Actually dynamic binding takes into account the signature excluding the returning type so Java compiler cannot accept the two methods for the same class that have same signature but different return types. If two methods have same names and same parameters than they MUST also have same returning type, unfortunately for you.

The only way is to split the behavior in two different classes and composing them. Maybe a method like Collection<V> asCollection() or something like that.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Here is the documentation to the JLS regarding method signature: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.2 – rit Oct 19 '11 at 09:41
1

No, there isn't a way to resolve such conflicts.

You should consider to use composition and delegation instead of inheritance for at least one of the two interfaces, or you could split the functionality of your class in two classes, it really depends on your concrete problem.

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • If I make my Map class extends Collection class and set the remove method final in the Collection class, I still get errors in the Map class. Or how should I use the composition? – Franz Kafka Oct 19 '11 at 09:31
  • If you make your "Map class extends Collection" you're using inheritance. If what you're trying to do is essentially a Map with some custom differences, make it extend only a Map, and if you need a Collection internally for some functionality, you can declare that Collection as a private variable, and also expose some of the needed methods, if necessary, by using delegation. You define a method that just pass the call on a similar method on your private variable. – stivlo Oct 19 '11 at 09:35
  • Thanks I get that, but when I use delegating my class looses the look of an real collection. It should be simple for the applications to use this implementation without worrying about having to call some toCollection() method. But this seems to be impossible. – Franz Kafka Oct 19 '11 at 09:41
1

The Map already has keySet() which is collection of keys. Why do you need the Collection also? If it's so, just do two methods like asMap and asCollecton which do return different types.

kan
  • 28,279
  • 7
  • 71
  • 101
0

You probably need composition instead of inheritance. Unfortunately Java has no language-level support for that - I mean it can be done but it is unnecessarily laborious.

Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
0

You need to rethink your design. Fundamentally, a map is different to a collection. Think about the Collection.add() method. Does it make any sense to add an object without a key or a key without a value to a map?

Your best bet (I think and depending on your application) is to implement a map but when you need a collection, use one of its methods to get the set of keys, values or key value pairs.

JeremyP
  • 84,577
  • 15
  • 123
  • 161