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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With