The xxx interface cannot be implemented several times with different arguments:

implements GenericObserver<DataService, Tools>, GenericObserver<MobileService, Tools> 

cannot be implemented multiple times with different arguments.

This is my interface:

 public interface GenericObserver<S, D> { void update(S sender, D data); } 

What can I do? I need a DataService and MobileService .

I am trying to use a generic T instead of a DataService and MobileService , but I get an error message that T does not exist.

+6
source share
4 answers

One possibility:

 abstract class Service {} class DataService extends Service {} class MobileService extends Service {} class Foo implements GenericObserver<Service, Tools> { void update(Service sender, Tools data) { if (sender instanceOf DataService) { // do something } else if (sender instanceOf MobileService) { // do something else } else { // throw some notImplemented exception } } } 

A visitor template is another opportunity ( GenericObserver is a visit).

+7
source

This is because Java does type erasure . After compilation, all typical types are erased, both interfaces end in the same way as in bytecode.

+13
source

The implementation class can provide these interfaces as capabilities that provide the lookup method, as shown below. The advantage of this template is the extensibility and decoupling of API classes.

 interface DataObserver extends GenericObserver<DataService, Tools> { } interface MobileObserver extends GenericObserver<MobileService, Tools> { } public class Implementor { private DataObserver dataObserver; private MobileObserver mobileObserver; public <T> T lookup(Class<T> klazz) { ... return dataObserver; } } 
+2
source

I see two possible scenarios why you are trying to do this:

This first - both GenericObserver and GenericObserver are required to implement the class interface. In this case, you probably have a design flaw because the objects should be made as specialized as possible (i.e. it is better to have two different classes, each of which focuses on a specialized task, rather than one class mixing them both) . Therefore, you should probably create two separate classes, one of which implements GenericObserver and the other GenericObserver. When you try to implement GenericObserver twice with different type arguments, the compiler complains because the generics in Java are implemented using erase, and therefore GenericObserver and GenericObserver are essentially the same GenericObserver at runtime.

Second scenario - these interfaces are part of your implementation details. Then you can use inner classes (or static nested classes) to achieve your goal. For instance:

 public class OuterClass { private String hello = "Hello"; private String world = "World"; private class InnerClass1 implements GenericObserver<DataService, Tools> { public String interfaceMethod() { return hello + " " + world; } // more implementation } private class InnerClass2 implements GenericObserver<MobileService, Tools> { // implementation } } 

If you use inner classes, you can easily access the fields of the inclusion class, as you can see from this slightly contrived example.

+1
source

All Articles