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() { / ... */ }
}

10
  • 1
    No. Due to the way generics are implemented in Java, you cannot do that. Commented Jul 25, 2013 at 4:01
  • 6
    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. Commented Jul 25, 2013 at 4:01
  • 5
    Which one is supposed to be used if someone calls MainClass.update()??? The two methods have the exact same signature! Commented Jul 25, 2013 at 4:01
  • Thanks. I'll try some other way. :/ Commented Jul 25, 2013 at 4:06
  • 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. Commented Jul 25, 2013 at 4:09

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.