Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NoArgsConstructor and AllArgsConstructor annotation on Child class for parent constructors

Hello to the overflow community, I am struggling on an inheritance problem with Lombok. I’m trying to add both annotation @AllArgsConstructor and @NoArgsConstructor on a child class in order to use the parent lombok constructors but got the error "Duplicate method Child()".

Parent class:

@ToString
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class Parent {
    private String propertyA;
    private String propertyB;
}

Child class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class Child extends Parent {
    @Override
    public void setPropertyA(String propertyA) {
        this.propertyA(StringUtils.upperCase(propertyA));
    }
}

The error message:

Duplicate method Child() in type Child Java(67109219)

Thanks to the @rentox98 reply, I understand that the ArgsConstructor on my child class would always be empty, resulting on two identical constructors.

Is there a Lombok way to generate ArgsConstructors on my child class based on the parent lombok ArgsConstructors ?

>Solution :

If you wanted an all-args constructor that would pass the properties to the parent class’s all-args constructor, you’d have to write it yourself; Lombok won’t generate it.

@SuperBuilder
@NoArgsConstructor
public class Child extends Parent {
    public Child(String propertyA, String propertyB) {
        super(StringUtils.upperCase(propertyA), propertyB);
    }
    
    @Override
    public void setPropertyA(String propertyA) {
        this.propertyA(StringUtils.upperCase(propertyA));
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading