Explain Codes LogoExplain Codes Logo

How to Call Super Constructor in Lombok

java
lombok
constructor
inheritance
Nikita BarsukovbyNikita Barsukov·Feb 15, 2025
TLDR

Invoke the super constructor in a class utilizing the @SuperBuilder annotation and calling superBuilder() in the subclass constructor. Always remember, both parent and child classes must be annotated with @SuperBuilder. Here is a practical example:

// Parent class sprinkled with a bit of @SuperBuilder @SuperBuilder public class Parent { private String parentName; } // Child class has superpowers, literally! @SuperBuilder public class Child extends Parent { private String childName; public Child(String parentName, String childName) { super(superBuilder().parentName(parentName)); // Calling mom first this.childName = childName; // And then initiating own affairs } }

Create a Child object in a breeze:

Child child = new Child("ParentName", "ChildName"); // Like a boss!

Following such patterns ensures that subclass fields are correctly initialized.

Advanced construction techniques with Lombok

Having covered the basics, let's delve deeper into more complex construction scenarios using the power of Lombok.

Clearing up the constructor chaos

In situations where the inheritance hierarchy gets complex, constructor chaining could come to the rescue:

public class Grandparent { protected final String name; public Grandparent(String name) { this.name = name; } } public class Parent extends Grandparent { // No favoritism! Calling our old man first public Parent(String name) { super(name); } } @NoArgsConstructor public class Child extends Parent { private String childSpecificField; // Child-exclusive drama to be added here }

In this approach, @NoArgsConstructor plays a crucial role in structuring the hierarchy. It orchestrates readability and constructor definitions.

Solidifying your security with immutability

Adding final to superclass and subclass members strengthens the security and reliability of your code:

public class ImmutableParent { private final String parentProperty; // Parent decided to never change this! That's so typical. public ImmutableParent(String parentProperty) { this.parentProperty = parentProperty; } } @AllArgsConstructor @SuperBuilder public class ImmutableChild extends ImmutableParent { private final String childProperty; // Lombok's magic trick - poof goes the constructor! }

Using final fields, together with Lombok's @AllArgsConstructor, allows for efficient initialization and maintains immutability.

The Unholy matrimony of Lombok and JPA

Combining Lombok annotations with Spring Data JPA refines entity definitions:

@Entity @Data @NoArgsConstructor @AllArgsConstructor @SuperBuilder public class EntityParent { @Id private Long id; private String description; // Hibernate enters the room } @EqualsAndHashCode(callSuper = true) @Entity public class EntityChild extends EntityParent { // Adding some spice with child specific attributes private String childSpecificAttribute; }

The @Data annotation sheds the boilerplate baggage by handling getters, setters, and hashCode methods. @NoArgsConstructor and @AllArgsConstructor, on the other hand, sit at the constructor's table with harmony.

Unveiling the advanced side of Lombok

Let's lift the curtain on more advanced ways to exploit Lombok functionalities in an inheritance marathon.

Maintaining your privacy using inner classes

Encapsulate logic of classes using inner classes:

public class A { @SuperBuilder public static class AInner { // It's getting cozier in here with encapsulation! } } public class B extends A.AInner { @SuperBuilder public static class BInner { // Taking encapsulation to a whole new level } }

This pattern constructs a structured hierarchy within your classes and promotes an air of encapsulation and modularity.

Through the looking glass with @Delegate

Embrace the**@Delegate** annotation when you want to resemble a has-a relationship rather than is-a:

public class Base { public void baseMethod() { /* Implementation */ } } public class Child { @Delegate private Base base; // Even a child has tasks these days! }

Consistency is key with parent-child annotations

The consistent application of @SuperBuilder and @Data between parent and child classes brings simplicity to your code:

@SuperBuilder @Data public class ConsistentParent { /* fields and methods */ } @SuperBuilder @Data public class ConsistentChild extends ConsistentParent { /* more fields and methods */ }

This consistent style reaps the benefits of Lombok's rich features, including automatic generation of equals, hashCode, and toString implementations.