Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a class to override the .equals method

I have a bunch of class who implement a common interface : Command.

And this bunch of class goes to a Map.

To get the Map working correctly, I need to each class who implements Command to override the Object.equals(Object other) method.

it's fine.

But i whould like to force the overriding of equals. => Have a compilation error when something who implement command dont override equals.

It's that possible ?

Edit : BTW , i will also need to forcing the override of hashcode...

like image 532
Antoine Claval Avatar asked Oct 23 '09 09:10

Antoine Claval


People also ask

Can we override a equals () and hashCode () method How?

if you override equals, you must override hashCode. hashCode must generate equal values for equal objects. equals and hashCode must depend on the same set of significant fields . You must use the same set of fields in both of these methods.

How do you override equals method in object class?

To compare two objects the object class provides a method with name equals(), this method accepts an object and compares it with the current object. If the references of these two objects are equal, then it returns true else this method returns false.

Can we override the equals method for the string class?

The String class overrides the equals method it inherited from the Object class and implemented logic to compare the two String objects character by character. The reason the equals method in the Object class does reference equality is because it does not know how to do anything else.


3 Answers

No, you can't. What you can do, however, is use an abstract base class instead of an interface, and make equals() abstract:

abstract class Command {
   // put other methods from Command interface here

   public abstract boolean equals(Object other);
   public abstract int hashCode();
}

Subclasses of Command must then provide their own equals and hashCode methods.

It's generally bad practice to force API users to extend a base class but it may be justified in this case. Also, if you make Command an abstract base class instead of an interface, rather than introducing an artificial base class in addition to the Command interface, then there's no risk of your API users getting it wrong.

like image 72
skaffman Avatar answered Oct 10 '22 07:10

skaffman


Can you extend your objects from an abstract XObject rather than java.lang.Object ?

public abstract class XObject
 extends Object
{
@Override
public abstract boolean equals(Object o);
}
like image 23
Pierre Avatar answered Oct 10 '22 06:10

Pierre


Abstract classes won't work if you have a grandchild since its father already overrided both equals and hashCode methods and then you have your problem all over again.

Try using annotatins and APT (http://docs.oracle.com/javase/1.5.0/docs/guide/apt/GettingStarted.html) to get it done.

like image 32
Philippe Gioseffi Avatar answered Oct 10 '22 07:10

Philippe Gioseffi