Lombok
Java
super constructor
programming
software development

how to Call super constructor in Lombok

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Lombok is a popular Java library that helps reduce boilerplate code through various annotations. Among its many features, Lombok offers annotations like @Data, @Getter, @Setter, @ToString, @EqualsAndHashCode, and more. These annotations streamline the process of creating Java classes by automatically generating methods, constructors, and more at compile time. When it comes to inheritance, handling superclass constructor calls is often a nuanced task. This article explores the process of calling a superclass constructor in Lombok, including technical explanations and examples.

Understanding the Basics

When creating a subclass in Java that needs to invoke a constructor from its superclass, you typically use the super() keyword. Lombok provides several annotations that can maintain this capability while also significantly reducing boilerplate constructor code.

Key Lombok 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.

Detailed Example

Consider a basic inheritance scenario. We will create a base class Person and a subclass Employee. We'll utilize Lombok features in the subclass to call the superclass constructor.

Base Class: Person

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}

Subclass: Employee

In the Employee class, we want to invoke the Person constructor using Lombok. Here's how it can be done:

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

By employing the @AllArgsConstructor annotation, Lombok generates a constructor for each field in the Employee class. However, since Employee also inherits fields from Person, you'll need to explicitly call the superclass constructor within the generated or manually defined constructor in Employee.

Advanced Usage: Leveraging Lombok Builders

In scenarios where constructors become complex due to many fields or optional parameters, using the @Builder annotation is advantageous.

Example with @Builder

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}

Here, the @Builder annotation not only provides a fluent API for creating instances of Manager, but we also retain control over the superclass constructor invocation.

Summary Table

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.

Additional Tips

  • Mixing Annotations: You can combine different Lombok annotations to address various constructor needs while maintaining clean code.
  • Customization: Lombok allows you to define your own constructor logic inside @Builder or any annotated constructors, making it highly flexible.
  • Visibility Control: Annotations like @AllArgsConstructor have parameters to control constructor visibility (access parameter).

Conclusion

Integrating Lombok in your Java application can dramatically reduce the verbosity associated with constructors and, by extension, constructor chaining and superclass constructor calls. While Lombok abstracts much of the underlying boilerplate, retaining control over explicit superclass calls ensures your inheritance hierarchies remain robust and correctly aligned with design expectations. By understanding and judiciously applying Lombok annotations, you can maintain both code clarity and functionality in complex Java class structures.


Course illustration
Course illustration

All Rights Reserved.