nano_nano
nano_nano

Reputation: 12523

generic interface method and implementation classes

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

Upvotes: 1

Views: 107

Answers (1)

Eran
Eran

Reputation: 393946

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) {}
}

Upvotes: 5

Related Questions