Here's an example of what I'd like to be able to do (in Java):
interface MyInterface<E> {
public void doSomething(E foo);
}
class MyClass implements MyInterface<ClassA>, MyInterface<ClassB> {
public void doSomething(ClassA fooA) {
...
}
public void doSomething(ClassB fooB) {
...
}
}
When I try to implement this, the compiler tells me that I can only implement MyInterface
once in MyClass
. I had assumed that MyInterface<ClassA>
is treated as a different type compared to MyInterface<ClassB>
, but apparently not. This seems like something that would be very useful, so I'm surprised by the error.
Update: Thanks for the help so far. As one of the comments pointed out, the above is abstract; the problem I am trying to solve is to implement the Observer pattern in such a way that an observer class declares those classes it observes via the interfaces it implements and also deals with changes in the objects it observes in class-specific methods. The advantage of using such an approach would be that it is clear from the interfaces a class implements which type of objects it observes, and so that compiler forces the programmer to explicitly implement the handling of changes to instances of those classes that are observed. So, the above abstract code might actually be something like the following:
interface IsObserverOf<E> {
public void observedDidChange(E foo);
}
class MyClass implements IsObserverOf<Database>, IsObserverOf<Spreadsheet> {
public void observedDidChange(Database database) {
...
}
public void observedDidChange(Spreadsheet spreadsheet) {
...
}
}
Here, MyClass declares that it observes Database objects and Spreadsheet objects, and the compiler would—in an ideal world—mandate that the two different observedDidChange() methods must be implemented.
Thanks.