Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java overriding two interfaces, clash of method names

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?

like image 781
Franz Kafka Avatar asked Oct 19 '11 09:10

Franz Kafka


2 Answers

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.

like image 90
Jack Avatar answered Oct 13 '22 04:10

Jack


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.

like image 31
kan Avatar answered Oct 13 '22 02:10

kan