Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, declare variable with multiple interfaces?

Tags:

java

In Java, is it possible to declare a field/variable whose type is multiple interfaces? For example, I need to declare a Map that is also Serializable. I want to make sure the variable references a serializable map. The Map interface does not extend Serializable, but most of Map's implementations are Serializable.

I'm pretty sure the answer is no.

Follow up: I'm fully aware of creating a new interface that extends both Map and Serializable. This will not work as existing implementations (such as HashMap) do not implement my new interface.

like image 413
Steve Kuo Avatar asked Jul 13 '09 20:07

Steve Kuo


People also ask

Can a class implement two interfaces with same variable names?

14) A class cannot implement two interfaces that have methods with same name but different return type. 15) Variable names conflicts can be resolved by interface name.

How do you write multiple interfaces in Java?

Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

What happens when a class implements two interfaces and both declare field variable with same name?

If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. If, say, the two methods have conflicting return types, then it will be a compilation error.

Can we implement multiple interfaces with same method in Java?

No, its an errorIf two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.


3 Answers

You can do it with generics, but it's not pretty:

class MyClass<T,K,V extends Serializable & Map<K,V>> {

   T myVar;

}
like image 197
skaffman Avatar answered Dec 02 '22 19:12

skaffman


There is no need to declare the field/variable like that. Especially since it can only be tested runtime and not compile time. Create a setter and report an error should the passed Map not implement Serializable.

The answers recommending that you create your own interface are of course not very practical as they will actively prohibit sending in things that are Maps and Serializable but not your special interface.

like image 35
Fredrik Avatar answered Dec 02 '22 20:12

Fredrik


It's possible to do this using some generics tricks:

    public <T extends Map<?,?> & Serializable> void setMap(T map)

The above code uses generics to force you to pass a map which implements both interfaces. However, note that a consequence of this is that when you actually pass it the maps, they will probably need to be either marked as serializable or of a map type which is already serializable. It also is quite a bit more difficult to read. I would document that the map must be serializable and perform the test for it.

like image 38
deterb Avatar answered Dec 02 '22 20:12

deterb