0

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 ?

  • By the way, the only difference between using `@Data` and using all of `@Getter`, `@Setter`, and `@ToString` is that the former also gets you `@EqualsAndHashCode` so unless there's some reason you want to avoid having Lombok generate the `equals` and `hashCode` methods on `Parent`, using just `@Data` is a lot more terse. – David Conrad Feb 04 '22 at 18:45
  • 1
    Hey, can you accept @rentox98's answer now, instead? And then I'll just delete mine. Thanks. – David Conrad Feb 04 '22 at 18:46

1 Answers1

6

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));
    }
}
rentox98
  • 361
  • 1
  • 4
  • 10
  • 1
    No need to apologize, this is a good answer and the right answer. +1 – David Conrad Feb 04 '22 at 18:24
  • Thanks, now I also achieved the privileges to comment. – rentox98 Feb 04 '22 at 18:26
  • 1
    @DavidConrad : what if the goal was to have a constructor in the Child class that passes properties to the Parent class ? – nquincampoix Feb 04 '22 at 18:29
  • thx for the reply ! I can now understand the error :) But how could I use the allArgsConstructor generated in my parent class ? – Ceci Semble Absurde. Feb 04 '22 at 18:31
  • 1
    @nquincampoix I think you would have to write that constructor explicitly. There's no inheritance of constructors in Java, and I don't know of a way to get Lombok to generate it for you. But I think that's another question. – David Conrad Feb 04 '22 at 18:31
  • @DavidConrad I agree, this is not possible with lombok (https://stackoverflow.com/questions/29740078/how-to-call-super-constructor-in-lombok/29771875) I just aksed in case that was what CeciSembleAbsurde wanted to do. – nquincampoix Feb 04 '22 at 18:33
  • 1
    @nquincampoix I added an answer for that case. Note to rentox98: if you want to incorporate that into your answer in addition to what you already have, feel free, and then I'll delete mine and we'll have one answer that covers all the bases. – David Conrad Feb 04 '22 at 18:39
  • @DavidConrad Ok, Thanks. – rentox98 Feb 04 '22 at 18:44