Note that there are some explanatory texts on larger screens.

plurals
  1. POOnly one parameterization of an interface can be implemented—how to work around?
    primarykey
    data
    text
    <p>Here's an example of what I'd like to be able to do (in Java):</p> <pre><code>interface MyInterface&lt;E&gt; { public void doSomething(E foo); } class MyClass implements MyInterface&lt;ClassA&gt;, MyInterface&lt;ClassB&gt; { public void doSomething(ClassA fooA) { ... } public void doSomething(ClassB fooB) { ... } } </code></pre> <p>When I try to implement this, the compiler tells me that I can only implement <code>MyInterface</code> once in <code>MyClass</code>. I had assumed that <code>MyInterface&lt;ClassA&gt;</code> is treated as a different type compared to <code>MyInterface&lt;ClassB&gt;</code>, but apparently not. This seems like something that would be very useful, so I'm surprised by the error.</p> <p>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:</p> <pre><code>interface IsObserverOf&lt;E&gt; { public void observedDidChange(E foo); } class MyClass implements IsObserverOf&lt;Database&gt;, IsObserverOf&lt;Spreadsheet&gt; { public void observedDidChange(Database database) { ... } public void observedDidChange(Spreadsheet spreadsheet) { ... } } </code></pre> <p>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.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload