0

I am trying to use Lombok to minimise the line of codes I have to write to convert superclass object to a subclass object.

SuperClass:

@Data
public class SuperClass{
// lots of fields
}

SubClass:

@Data
public class SubClass extends SuperClass{
private String additionalField;

public SubClass(SuperClass super, String additionalField){
  // set all available fields from superclass
  this.additionalField = additionalField;
  }
}

I trivial solution is to accept all arguments and set them accordingly, but I am looking for a minimum model that I need to only pass the additional arguments to the constructor.

Ali
  • 1,759
  • 2
  • 32
  • 69
  • possible duplicate of https://stackoverflow.com/questions/29740078/how-to-call-super-constructor-in-lombok. Also, not sure how this is relevant to `downcasting` – Milad Apr 16 '20 at 14:20
  • That question is actually touching on a different aspect, so I don't think it helps. – Ali Apr 16 '20 at 14:32
  • Does `SuperClass` have an existing constructor that accepts `//lots of fields`? – Sean Mickey Apr 16 '20 at 19:27
  • @SeanMickey I can add '@AllArgsConstructor' to have that contractor, but it means I need to pass every field one by one in the SubClass constructor. I am mostly looking for an approach to take the SuperClass object and set them accordingly. – Ali Apr 16 '20 at 23:33

0 Answers0