简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM