Inheritance for builders in lombok

Abhinav Aggarwal picture Abhinav Aggarwal · Jun 10, 2016 · Viewed 13.8k times · Source

I was trying to use lombok for my project.

I have a class A:

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

and a class B:

@Data
public class B extends A {
    Integer b1;

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

I am getting an error saying builder() in B cannot override builder() in A, as return type in BBuilder is not compatible with return type in ABuilder.

Is there some way to do this using lombok? I do not want to write the complete builder for for B, unless I don't have any other option.

PS: I have given explicit constructor for class B due to Issue. I tried searching, but I could not find a good solution for the same.

Answer

Abhinav Aggarwal picture Abhinav Aggarwal · Jun 10, 2016

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();
            }
    }
}