Problem with equals() method in inheritance that violates the symmetric behavior in Java

Abhijit Jadhav
2 min readJul 27, 2022

--

When we Inherit the class and override the equals method then the instanceof will violate the symmetric behavior that the equals method should follow according to the contract.

Problem with equals() method in inheritance that violates the symmetric behavior in Java

Let's see first what the contract of the equals method says. The equals method should be

  • Reflexive: for any non-null reference value x.equals(x) should be True
  • Symmetric : for any non-null value x.equals(y) and y.equals(x) both should be True.
  • Transitive: for any non-null value x.equals(y), y.equals(z), z.equals(x) all should be True
  • Consistent: for multiple invocations of x.equals(y) should always return the same value.

Now let's see how instanceof violates the symmetric behavior of the equals method during inheritance.

class Dog{
private String name;

public Dog(String name) {
super();
this.name = name;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof Dog)
return true;
return false;
}
}
class Labrador extends Dog{
public Labrador(String name) {
super(name);
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof Labrador)
return true;
return false;
}
}
public class Main {
public static void main(String[] args) {
Labrador rover1 = new Labrador("Rover");
Dog rover2 = new Dog("Rover");
System.out.println(rover1.equals(rover2));
System.out.println(rover2.equals(rover1));
}
}

Output:

false
true

Ideally, the output of both statements should be true.

To avoid this issue we need to make the equals() and hashcode() method final.

class Dog{
private String name;

public Dog(String name) {
super();
this.name = name;
}

@Override
public final boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof Dog)
return true;
return false;
}
}
class Labrador extends Dog{
public Labrador(String name) {
super(name);
}
}
public class Main {
public static void main(String[] args) {
Labrador rover1 = new Labrador("Rover");
Dog rover2 = new Dog("Rover");
System.out.println(rover1.equals(rover2));
System.out.println(rover2.equals(rover1));
}
}

Output:

true
true

This is how we can resolve the violation of symmetric behavior in the equals method during inheritance.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Abhijit Jadhav
Abhijit Jadhav

Written by Abhijit Jadhav

6X AWS Certified Developer and AI Enthusiast love to build latency-sensitive scalable applications with the latest tech stack. Tech Skills - AWS, Java, and AI

Responses (1)

Write a response