Skip to content
Advertisement

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:

@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 ?

Advertisement

Answer

In your Child class you have no attributes, so @NoArgsConstructor and @AllArgsConstructor are the same and the error occurs.


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));
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement