Inheritance for builders in lombok

Both child and parent should be marked with @SuperBuilder.

Having both parent and child as @Builder won't work.

Parent class A:

@Data
@SuperBuilder
public class A {
    Integer a1;
}

Child class B:

@Data
@SuperBuilder
public class B extends A {
    Integer b1;
}

If you are using Lombok 1.18.4 along with IntelliJ, following code shall work for you:

@Data
@Builder
class A {
    Integer a1;
}

@Data
class B extends A {
    Integer b1;

    @Builder (builderMethodName = "BBuilder")
    public B(Integer b1, Integer a1) {
        super(a1);
        this.b1 = b1;
    }
}

public class Main {

    public static void main(String[] args){
    System.out.println(B.BBuilder().a1(1).b1(1).build());

    }
}

One a side note, @SuperBuilder annotation didn't work in IntelliJ at time of writing this answer. If you have multiple level of inheritance, please avoid Lombok or it will make your Java models messy.


Lombok has introduced experimental features with version: 1.18.2 for inheritance issues faced with Builder annotation, and can be resolved with @SuperBuilder annotation

Please use lombok version: 1.18.2, @SuperBuilder annotations in child/parent class


Here we just need to call super of the builder.

@Data
public class B extends A {
    Integer b1;

    @Builder
    public B(Integer b1, Integer a1) {
        super(a1);
        this.b1 = b1;
    }

    public static class BBuilder extends ABuilder{
            BBuilder() {
                super();
            }
    }
}