1

I've following codes.


interface Observer<T> {
    void update();
}


interface FirstClassObserver extends Observer<FirstClass>{ }


interface SecondClassObserver extends Observer<SecondSecond> { }

Now, I'm required to do as follows.


class MainClass implements FirstClassObserver, SecondClassObserver {
}

But Eclipse give following problem with the code.

The interface Observer cannot be implemented more than once with different arguments: FirstClassObserver<FirstClass> and SecondClassObserver<SecondClass>

Is there a way that I can write my MainClass like


class MainClass implements FirstClassObserver, SecondClassObserver {
   @Override
   void FirstClassObserver::update() { /* ... / }
   @Override
   void SecondClassObserver::update() { / ... */ }
}
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • 1
    No. Due to the way generics are implemented in Java, you cannot do that. – Thilo Jul 25 '13 at 04:01
  • 5
    Because of [type erasure](http://docs.oracle.com/javase/tutorial/java/generics/erasure.html), you aren't going to be able to do this. Basically, all generic type parameters end up as `Object` in the compiled byte code. – Ted Hopp Jul 25 '13 at 04:01
  • 5
    Which one is supposed to be used if someone calls MainClass.update()??? The two methods have the exact same signature! – assylias Jul 25 '13 at 04:01
  • Thanks. I'll try some other way. :/ – TheKojuEffect Jul 25 '13 at 04:06
  • How do I tag this question as "Closed" ? – TheKojuEffect Jul 25 '13 at 04:07
  • 1
    @TheKojuEffect You can post your own answer and then accept it. You should also be able to simply delete the question, if you think that's appropriate. – Ted Hopp Jul 25 '13 at 04:09
  • @TedHopp Other people may get this type of issue, so I'm not gonna delete question but will write an answer. – TheKojuEffect Jul 25 '13 at 04:41
  • @assylias While developing in C++, we do manage to handle this type of situation. Also since Java being single inheritance with class but multiple inheritance with inheritance, thought it will be fine. – TheKojuEffect Jul 25 '13 at 04:43
  • What you want to do doesn't make sense (in my opinion). Once you have to make up new syntax to support something, it's a good sign it's not going to work. – jahroy Jul 25 '13 at 05:00
  • possible duplicate of [Implementing multiple generic interfaces](http://stackoverflow.com/questions/11290558/implementing-multiple-generic-interfaces). See also: [How to make a Java class that implements one interface with two generic types?](http://stackoverflow.com/questions/1297972/how-to-make-a-java-class-that-implements-one-interface-with-two-generic-types) – Paul Bellora Jul 25 '13 at 14:54

1 Answers1

3

According to @Ted's comment. Because of type erasure, you aren't going to be able to do this. Basically, all generic type parameters end up as Object in the compiled byte code.

Also according to @assylias's comment, there can be ambiguous situation as whose instance of update() method to call MainClass.update() is invoked.

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125