dendimiiii :
I am trying to achieve the next:
Parent:
public class Animal {
private List<Animal> relatives;
public List<Animal> getRelatives() {
return relatives;
}
public void setRelatives(List<Animal> relatives) {
this.relatives = relatives;
}
}
Child:
public class Dog extends Animal {
private List<Dog> relatives;
public List<Dog> getRelatives() {
return relatives;
}
public void setRelatives(List<Dog> relatives) {
this.relatives = relatives;
}
}
But for some reason, I get errors that the methods are clashing. Is this even possible with inheritance, without using generic types?
Andrew Tobilko :
You don't really need to override/duplicate anything in the child class.
class Animal<Relative extends Animal<?>> {
private List<? extends Relative> relatives;
public List<? extends Relative> getRelatives() {
return relatives;
}
public void setRelatives(List<? extends Relative> relatives) {
this.relatives = relatives;
}
}
class Dog extends Animal<Dog> {}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=310555&siteId=1