1

The following example illustrates my problem (I want the mother of a Lion to be a Lion, the mother of a Monkey to be a Monkey and so on).

public class Animal<T extends Animal> {

    private T mother;

    public void setMother(T mother) {
        this.mother = mother;
    }

    public void receiveChild(T child) {
        child.setMother(this);
    }

}

The compiler gives me the following warning:

Animal.java:10: warning: [unchecked] unchecked call to setMother(T) as a member
of the raw type Animal
                child.setMother(this);
                               ^
  where T is a type-variable:
    T extends Animal declared in class Animal

Is it possible to do this in a type safe way?

Markus
  • 23
  • 3

0 Answers0