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; }
If you use inner classes, you can easily access the fields of the inclusion class, as you can see from this slightly contrived example.
source share