Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

During a code review you find a class hierarchy where class Admin extends User, both annotated @Data, and admins with different permissions compare as equal. What is wrong and what is the right configuration?

Lombok's generated equals and hashCode do not call the superclass by default, so with @Data on Admin, two admins are compared only on the fields declared in Admin and the permissions stored in User are ignored. Lombok even warns about this at compile time. The fix is @EqualsAndHashCode(callSuper = true) on the subclass, or lombok.equalsAndHashCode.callSuper = call in lombok.config as a project rule, and the deeper fix is to decide deliberately whether a data class should use inheritance at all.

Why this question is asked

Equality bugs in class hierarchies are hard to spot in review because the code that causes them is not in the file. Two admins with different permissions compare equal, so a Set of admins drops one, a cache returns the wrong one, or a change detector thinks nothing changed. Interviewers ask this scenario to check whether we know Lombok's defaults and the rules of equals in a hierarchy. In this answer, we will recap what equals must guarantee, reproduce the bug, see why Lombok ignores the superclass, fix it per class and per project, look at the canEqual method that keeps equality symmetric, and finally ask whether inheritance is the right tool here.

1A Quick Recap of the equals Contract

equals must be reflexive (x equals x), symmetric (if x equals y then y equals x), transitive, and consistent, and equal objects must have equal hash codes. In a class hierarchy, the hardest part is symmetry: a User compared with an Admin and an Admin compared with a User must give the same answer.

Lombok's @Data includes @EqualsAndHashCode, which generates equals and hashCode from the fields declared in that class. The question is what happens to the fields declared in the superclass.

2Reproducing the Bug

In our hierarchy, User holds the identity and the permissions, and Admin adds the department it manages.

Java
@Data
public class User {
private String email;
private Set<String> permissions;
}
@Data // warning: Generating equals/hashCode implementation but
public class Admin extends User { // without a call to superclass ...
private String department;
}
Admin a = new Admin(); a.setEmail("ana@x.com"); a.setPermissions(Set.of("READ")); a.setDepartment("IT");
Admin b = new Admin(); b.setEmail("bo@x.com"); b.setPermissions(Set.of("READ", "DELETE")); b.setDepartment("IT");
System.out.println(a.equals(b)); // true
System.out.println(new HashSet<>(List.of(a, b)).size()); // 1: one admin disappeared
System.out.println(a); // Admin(department=IT)

Notice that even toString hides the problem: it prints only department, because @ToString also ignores the superclass by default. Two different people with different permissions look like the same object.

3Why Lombok Ignores the Superclass

By default, Lombok generates equals with callSuper = false: it compares only the fields of the class it is generating for, and does not call super.equals(). The reason is that Lombok cannot know whether the superclass has a meaningful equals. If the superclass is Object, calling super.equals() would compare identity, and no two different objects would ever be equal.

Because skipping the superclass is often wrong when there is a real superclass, Lombok prints a warning when a class extends something other than Object and callSuper is not set. That warning is the clue to look for in the build log; in many projects it scrolls by among hundreds of others.

Step throughWhat a.equals(b) does with the default
CallerAdmin aAdmin b
a.equals(b)
b instanceof Admin? yes
b.canEqual(a)? yes
compare department: IT == IT
true; email and permissions never compared

5 messages in order. Play them, or step through them one at a time.

The generated equals never looks at the fields declared in User.

4Fix 1: callSuper = true on the Subclass

We tell Lombok to include the superclass's equality. The generated equals then first calls super.equals(other), and hashCode mixes in super.hashCode().

Java
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class Admin extends User {
private String department;
}
System.out.println(a.equals(b)); // false: permissions and email differ
System.out.println(a); // Admin(super=User(email=ana@x.com, permissions=[READ]), department=IT)

This only works if the superclass has a value-based equals of its own, as User does here through @Data. If User relied on Object's equals, callSuper = true would make every two different admins unequal, which is also wrong. So the decision depends on the superclass.

5Fix 2: Make It a Project Rule in lombok.config

Relying on every developer to remember the attribute does not scale. Lombok reads a lombok.config file from the source directories, and one key controls this default.

Properties
# lombok.config at the project root
config.stopBubbling = true
lombok.equalsAndHashCode.callSuper = call # call | skip | warn (the default is warn)
lombok.toString.callSuper = call

With call, Lombok includes the superclass in every class that extends something other than Object. Classes that extend Object are unaffected. We still review each hierarchy, but the default now matches what most people expect.

6Why canEqual Exists, and Whether to Use Inheritance at All

Lombok also generates a protected method canEqual(Object other) that returns other instanceof ThisClass, and equals calls other.canEqual(this). It keeps equality symmetric across a hierarchy: user.equals(admin) asks admin.canEqual(user), which is false because a User is not an Admin, and admin.equals(user) is false too because user is not an instance of Admin. Without it, a parent could say yes while the child says no.

The deeper question is whether Admin should extend User at all. Data classes with inheritance always raise this equality question. Composition is often clearer: an Admin record that holds a User and a department, or a User with a role field. Records avoid the problem completely, because they cannot extend other classes and their equality is defined by their components. @Data is a convenience that hides the equality decision; in a hierarchy we should make that decision on purpose.

Java
public record User(String email, Set<String> permissions) {}
public record Admin(User user, String department) {} // equality covers user and department

7Follow-up questions

Why doesn't Lombok call super by default?

Because it cannot know whether the superclass's equals compares values or identity. For a class that extends Object, calling super would break equality entirely. So Lombok chooses the safe default for Object and warns when the class has a real superclass, leaving the decision to us.

What does callSuper do to hashCode?

It starts the hash calculation from super.hashCode() and then mixes in the subclass's fields, so equal objects keep equal hash codes across the whole hierarchy.

Can a User ever equal an Admin with the same email and permissions?

Not with Lombok's generated code, because canEqual makes the comparison false in both directions. That is usually what we want: an object of a more specific type is a different kind of thing.

8Conclusion

In this answer, we saw that Lombok's generated equals and hashCode ignore the superclass by default, so Admin compared only its own department and admins with different permissions looked equal. We fixed it with callSuper = true per class or lombok.equalsAndHashCode.callSuper = call in lombok.config, noted that this needs a value-based equals in the superclass, saw how canEqual keeps equality symmetric, and questioned whether data classes need inheritance at all, since composition or records avoid the problem.

What to say in the interviewAbout 80 seconds aloud

Open with

The issue is Lombok's default for equals and hashCode: callSuper is false, so the generated equals in Admin compares only the fields Admin declares, here the department, and never calls User's equals.

Then, if they want more

Email and permissions live in User, so two different admins in the same department compare equal, a HashSet keeps only one, and toString hides it too because it also skips the superclass. Lombok actually warns about this at compile time, and that warning is easy to miss.

The fix on the class is @EqualsAndHashCode(callSuper = true), and @ToString(callSuper = true) for logs; that works because User has a value-based equals of its own. To make it a rule, I would set lombok.equalsAndHashCode.callSuper = call in lombok.config, which only affects classes that extend something other than Object.

I would also mention that Lombok generates canEqual so equality stays symmetric between parent and child: a User never equals an Admin in either direction. In the review, though, my main comment would be about the design. Inheritance between data classes always forces this decision, and @Data hides it.

Often composition is clearer, an Admin that holds a User, or records, which cannot extend classes and define equality by their components.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "During a code review you find a class hierarchy where class Admin extends User, both...": https://scrivio-interview-library.netlify.app/java/lombok/51b92700d4/