I'm modeling something in Java and using a Builder pattern. In a number of cases, some common members are defined in a parent, with additional members on children that inherit from the parent. An example is as follows:
public class Parent {
private Integer age;
static class ParentBuilder {
private Integer age;
public ParentBuilder age(Integer age) {
this.age = age;
return this;
}
}
}
and
public class Child extends Parent {
private Integer height;
static class ChildBuilder extends Parent.ParentBuilder {
private Integer height;
public ChildBuilder height(Integer height) {
this.height = height;
return this;
}
public Child build() {
return new Child(this);
}
}
public static ChildBuilder builder() {
return new ChildBuilder();
}
public Child(ChildBuilder b) {
this.height = b.height;
}
}
If I try to do something like
Child child = Child.builder()
.age(18)
.height(150)
.build();
I get an error trying to compile:
Main.java:6: error: cannot find symbol
.height(150)
^
symbol: method height(int)
location: class ParentBuilder
If I remove .height(150)
I then get the same error on .build()
. It seems I have a fundamental misunderstanding of inheritance with static nested classes.
Why, when Child.builder()
returns a ChildBuilder
, is the compiler complaining about the method not being in ParentBuilder
? Is there a way to make this work as I'm attempting to, leveraging inheritance together with this Builder pattern to allow common members to be defined in the parent and others on the child?