1

I have this interface method in the Animal interface:

<T extends Animal> boolean isEqual(T pAnimal);

now I am in trouble with the implementation. I have two classes Dog and Cat. Both classes extends from Animal. I want to overwrite the method but with the concrete implementation:

@Override
public boolean isEqual(Dog pDog) {
  // do some stuff.
  return true;
}

this leads to the compile error

must override or implement a supertype method

How I have to define the interface so that it is not necessary to make a typecast in my concrete implementation class?

Thanks in advance

Stefan

1 Answer 1

5

It would work if you move the type parameter to the interface level :

public interface MyInterface<T extends Animal> {
    boolean isEqual(T pAnimal);
}

Then

public class Dog implements MyInterface<Dog> {
    @Override
    boolean isEquals (Dog dog) {}
}
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.