Project Lombok @Builder and inheritance

We have been using @Builder on the class itself, but you can also put it on a class’s constructor or on a static method. In that case, Lombok will create a setter method on the builder class for every parameter of the constructor/method. That means you can create a custom constructor with parameters for all the fields of the class including its superclass.

@AllArgsConstructorpublic class Parent {  private String a;} public class Child extends Parent {   private String b;   @Builder  private Child(String a, String b){    super(a);    this.b = b;  }}

By setting the visibility of the constructor to private you can make sure that it is only available to Lombok . As a result you can then use the generated builder like this:

Child.builder().a("testA").b("testB").build()