Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing hashCode() and equals() in class with no fields

I have an abstract class which will be used in a Hashtable:

public abstract class CEvent {

    abstract public void finished();
}

How to implement hashCode() if it does not have any field, only methods? Should i rely on Object implementation of hashCode?

like image 718
Hélène Avatar asked Jan 04 '16 09:01

Hélène


People also ask

How do we implement hashCode () and equals ()?

Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

What will happen if you did not override hashCode and equals method?

If you don't override hashcode() then the default implementation in Object class will be used by collections. This implementation gives different values for different objects, even if they are equal according to the equals() method.

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

"If two objects are equal using Object class equals method, then the hashcode method should give the same value for these two objects." So, if in our class we override equals() we should override hashcode() method also to follow this rule.

What is the relationship between hashCode () and equals () method in Java?

The hashCode() method should return the same integer value for the same object for each calling of this method unless the value stored in the object is modified. If two objects are equal(according to equals() method) then the hashCode() method should return the same integer value for both the objects.


3 Answers

If the class is abstract, then it must have concrete subclasses. You implement the hash code method in the concrete subclasses.

You should ensure that hashCode() is consistent with equals(). If two objects are considered equal they should return the same hash code. See the Java documentation on object for more details.

hashCode

You can implement hashCode in the abstract class if it can obtain the information required for hashing from abstract methods. The Java class AbstractList does this. However, you will need to be happy that by default, different derived classes will inherit that method and will return the same hash code for similar data. In your case, it does not make sense to implement the hashCode function in the abstract class.

like image 101
rghome Avatar answered Oct 31 '22 00:10

rghome


Actually, you can use the following as the default behaviour:

@Override
public boolean equals(Object o) {
   return this == o || o instanceof CEvent;
}

@Override
public int hashCode() {
    return CEvent.class.hashCode();
}
like image 26
Alexander Davliatov Avatar answered Oct 31 '22 00:10

Alexander Davliatov


Just use super class(Object) equals and hascode implementation in this case. this will have no effect on your code as you dont have anything to compare.

like image 1
Manish Mudgal Avatar answered Oct 31 '22 02:10

Manish Mudgal