Lombok
Java
super constructor
programming
software development

How to call a super constructor in Lombok

Codemia Knowledge Hub ·

To call a superclass constructor with Lombok, write the subclass constructor explicitly and call super(...) inside it. Fields inherited from the parent are not covered by Lombok's generated constructors, so the explicit call is what wires them up. You can still put @Builder on that hand-written constructor to get a fluent API.

Lombok constructor annotations involved

  • @AllArgsConstructor: Generates a constructor with 1 parameter for each field in the class.
  • @NoArgsConstructor: Generates a no-arguments constructor.
  • @RequiredArgsConstructor: Generates a constructor for each final field.
  • @Builder: Produces complex builder APIs for your classes.

Example: Employee extending Person

The base class has a regular constructor:

java
1public class Person {
2    private String name;
3    private int age;
4
5    // Constructor
6    public Person(String name, int age) {
7        this.name = name;
8        this.age = age;
9    }
10}

In the subclass, the constructor calls super(name, age) and then sets its own field:

java
1import lombok.Getter;
2
3@Getter
4public class Employee extends Person {
5    private String jobTitle;
6
7    // Explicit constructor: @AllArgsConstructor cannot call super(...)
8    public Employee(String name, int age, String jobTitle) {
9        super(name, age); // Call to the superclass constructor
10        this.jobTitle = jobTitle;
11    }
12}

@AllArgsConstructor generates a constructor for the fields declared in Employee only. Because Employee also inherits fields from Person, you need to call the superclass constructor explicitly in the constructor you define.

Calling super from a @Builder constructor

When constructors grow complex with many fields or optional parameters, put @Builder on the explicit constructor:

java
1import lombok.Builder;
2import lombok.Getter;
3
4@Getter
5public class Manager extends Person {
6    private String department;
7
8    @Builder
9    public Manager(String name, int age, String department) {
10        super(name, age); // Explicit super constructor call
11        this.department = department;
12    }
13}

You get a fluent builder for Manager and keep control over the superclass constructor call.

Annotation reference

Lombok AnnotationDescriptionUsage & Benefits
@AllArgsConstructorGenerates all fields constructorPerfect for straightforward classes with all fields as constructor args.
@NoArgsConstructorGenerates no-arguments constructorUseful when subclasses need a default constructor.
@RequiredArgsConstructorGenerates constructor for final fieldsEnsures all non-initialized final fields are set.
@BuilderOffers a builder pattern implementationIdeal for complex objects requiring conditional construction.

Other constructor options

  • Mixing annotations: You can combine Lombok annotations to cover different constructor needs.
  • Custom logic: You can define your own constructor logic inside @Builder or any annotated constructor.
  • Visibility control: Annotations like @AllArgsConstructor take an access parameter to control constructor visibility.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms