Compile error with generic types with circular generic dependencies

Consider the following code:

public abstract class Animal<A extends Cage<? extends Animal<A>>>
{
    private A cage;

    public A getCage()
    {
        return this.cage;
    }

    public void setCage(final A cage)
    {
         this.cage = cage;
    }
}

public abstract class Cage<A extends Animal<? extends Cage<A>>>
{
    private List<A> animals;

    public void addAnimal(final A animal)
    {
        if (animal != null)
        {
            animal.setCage(this);

            if (this.animals == null)
            {
                this.animals = new ArrayList<A>();
            }

            this.animals.add(animal);
        }
    }
}

      

These classes are further extended with other classes.

This code will not compile in line

animal.setCage(this);

      

Any thoughts on why this code won't compile and how you can fix it? We are using Java 1.6.

+3


source to share


3 answers


This should suit your needs:



public abstract class Animal<A extends Cage<C, A>, C extends Animal<A, C>> {

    private A cage;

    public A getCage() {
        return this.cage;
    }

    public void setCage(final A cage) {
        this.cage = cage;
    }

}

public abstract class Cage<A extends Animal<C, A>, C extends Cage<A, C>> {

    private List<A> animals;

    @SuppressWarnings("unchecked")
    public void addAnimal(final A animal) {
        if (animal != null) {
            animal.setCage((C) this);
            if (this.animals == null) {
                this.animals = new ArrayList<A>();
            }
            this.animals.add(animal);
        }
    }

}

      

-1


source


Effectively you are trying to say that A is the same in both classes, which it is not, and hence it will not compile.



I think you should consider exactly why you need generics here, I think it can certainly be easier.

0


source


For the methods you showed, this seems to be sufficient:

public abstract class Animal<C> {

    private C cage;

    public C getCage() {
        return this.cage;
    }

    public void setCage(final C cage) {
        this.cage = cage;
    }

}

public abstract class Cage<A extends Animal<C>, C> {

    private List<A> animals;

    public abstract C getThis();

    public void addAnimal(final A animal) {
        if (animal != null) {
            animal.setCage(getThis());
            if (this.animals == null) {
                this.animals = new ArrayList<A>();
            }
            this.animals.add(animal);
        }
    }

}

public class CatCage extends Cage<Cat, CatCage> {
    @Override
    public CatCage getThis() { return this; }
}

      

0


source







All Articles