implement nested abstract class with getter/setter implement nested abstract class with getter/setter json json

implement nested abstract class with getter/setter


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


There are several options how to de-serialize. If you have no annotations, Jackson probably falls back to using a constructor of nearest superclass that has a public constructor with no parameters. After creating an empty object, it then sets the fields using reflections.

  • Reflections WILL bypass any validations in constructor like Objects.requireNotNull.

A more refined way how to use Jackson is to use @JsonCreator and @JsonProperty in constructor, naming all the parameters.

  • This enables you to force Jackson to use a constructor you made and validate the object.

I am quite unsure about this rather unusual structure. After all, you have an abstract class nested in an abstract class, I have seen a lot of things, but I haven't seen anything like this. Is there a good reason? Or is it not a problem to just move the inner class to a file of its own? (it is public static after all)


If you create an instance of AA you need an instance of the abstract class B and you define nowwhere, which instance to use. Just providing some implementation (BB) isn't enough.