Amit Kaneria
Amit Kaneria

Reputation: 5808

Java Lombok library : builder annotation issues with Inheritance

Java Lombok library : builder annotation issues with Inheritance

    @Builder
    public class ParentClass {
        private final String a;
        private final String b;
    }

    @Builder
    public class ChildClass extends ParentClass{
        private final String c;
    }

When creating an instance of child class, parent class attributes are not visible with Builder annotations.

Below fails:

     ChildClass.builder().a("testA").b("testB").c("testC").build();

However, below statement is correct:

     ChildClass.builder().c("testC").build();

Seems this issue is open for long time, dont know if any latest release has any fixes.

Upvotes: 0

Views: 790

Answers (2)

Jan Rieke
Jan Rieke

Reputation: 8042

The latest lombok release 1.18.2 includes the new experimental @SuperBuilder. It was added exactly for this: setting fields from superclasses.

Upvotes: 2

Mike
Mike

Reputation: 932

The problem is that when you define ParentClass with @Builder annotation it creates ParentClass(String) constructor and deletes implicit one. Then Child class cannot even be created.

Please look at the following answer: how to Call super constructor in Lombok

Upvotes: 1

Related Questions