Important Note – Java equals Method and Inheritance

Important Note – Java equals Method and Inheritance

In this article, we will check a few important points on overriding equals() method when we are using inheritance. We consider two scenarios:

  • When semantics of equals changes in subclass
  • When superclass determines the semantics of equals and semantics of equals doesn’t change in subclass

We will discuss the point with concrete examples for quick understanding.

Scenario 1 – Semantics of equals changes in subclass

In this scenario, super class objects get checked for equality using some specific logic and sub class objects get checked for equality using different logic. Therefore, if you try to check if one super class object is equal to a sub class object (or vice versa) or not, you will always get false. Let's understand this using example code.

Suppose, you have designed a class named SuperClass as follows:

Article content

You have also designed another class SubClass that extends SuperClass as follows:

Article content

Now we need to check equality of objects of classes SuperClass and SubClass based on state (values of the instance fields at a particular time point).

For that purpose, we need to override Object’s equals() method both in SuperClass and SubClass.

One important point is that SubClass will have a different semantics for equality.         

Two SuperClass objects are equal when the values of instance fields v1 and v2 are same. But two SubClass objects are equal when the values of instance fields v1, v2, and v3 are same.

Moreover, we need to check the class of the given objects. Considering all these points, we can implement equals() method as follows:

Article content
Article content

Important Note: Since semantics of equals changes in subclass, we need to check the class of the objects inside equals() method.

Now consider the code snippet below:

Article content

If you run the above code snippet (with proper Java program structure in place), you will get the below output:

Article content


Scenario 2 – Superclass determines the semantics of equals and semantics of equals doesn’t change in subclass

In this scenario, super class decides the equality of any two objects (one super class object and one sub class object OR one super class object and another super class object OR one sub class object and another sub class object).

Let's understand this using example code.

Suppose, you have designed a class named SuperClazz with instance fields v1 and v2. You also have designed a SubClazz (extending SuperClazz) with one instance field v3. Here, you consider two objects of classes SuperClazz and SubClazz (one SuperClazz object and one SubClazz object OR one SuperClazz object and another SuperClazz object OR one SubClazz object and another SubClazz object) are equals if the value of the instance field v1 is same.

One important point is that subclass SubClazz must not change the semantics of equals as defined by SuperClazz class. Therefore, we won’t allow SubClazz class to override the equals() method.

Moreover, we don’t need to check the class of the given objects. Instead, we need to check whether the otherObject is an instance of SuperClazz class or not. Considering all these points, we can implement equals() method as follows:


Article content
Article content

Important Note: Since semantics of equals is fixed in superclass, we should use instanceof test inside the equals method.

Now consider the code snippet below:

Article content

If you run the above code snippet (with proper Java program structure in place), you will get the below output:

Article content

That’s it! I hope you find this article useful!

Meenakshi A.

Technologist & Believer in Systems for People and People for Systems

7mo

Very helpful to learn the concepts for the good 😊

Rachel Beck

The unforeseen is beautiful and, given a chance, can be more fulfilling than we can imagine | Author | Consultant | Speaker | Kindness changes everything

7mo

Good morning, my friend and people need to follow you and they need to subscribe to your newsletter and how are you doing?💜😃

To view or add a comment, sign in

More articles by Sanjoy Kumar Malik .

Insights from the community

Others also viewed

Explore topics