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
Subclass: Employee
In the Employee class, we want to invoke the Person constructor using Lombok. Here's how it can be done:
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
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 Annotation | Description | Usage & Benefits |
@AllArgsConstructor | Generates all fields constructor | Perfect for straightforward classes with all fields as constructor args. |
@NoArgsConstructor | Generates no-arguments constructor | Useful when subclasses need a default constructor. |
@RequiredArgsConstructor | Generates constructor for final fields | Ensures all non-initialized final fields are set. |
@Builder | Offers a builder pattern implementation | Ideal 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
@Builderor any annotated constructors, making it highly flexible. - Visibility Control: Annotations like
@AllArgsConstructorhave parameters to control constructor visibility (accessparameter).
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.

