j0h4nn3s j0h4nn3s - 3 years ago 92
Java Question

implement nested abstract class with getter/setter

I have the following abstract class structure:

public abstract class A {
...
private List<B> b;

public List<B> getB() {
return b;
}
public void setB(List<B> b) {
this.b = b;
}
public static abstract class B {

}
}


and implement it like this:

public class AA extends A {
public static class BB extends B {
...
}


When I now use the jackson to map Json on AA, I get an error, that it could not create an instance of A$B. I think this is, because the getter/setter in A still references on B and not on BB, which causes the error. Is there any way I can do it like that, without also putting the getter/setter in the subclass?

Answer Source

You could add type capture to make sure b is always correctly typed like this:

public abstract class A<T extends A.B> {
    ...
    private List<T> b;

    public List<T> getB() {
        return b;
    }
    public void setB(List<T> b) {
        this.b = b;
    }
    public static abstract class B {
        ...
    }
}

and then

public class AA extends A<AA.BB> {
    ...
    public static class BB extends B {
        ...
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download