How to override a equals() method and what is the use?
Answer Posted / rohan
equals is a method in Object class..
By default in java all the classes extend object class so
any two object can be compared using the equals method...
Provided user implement their own equals method(Override),
because the original equals method compares two objects by
their reference..
If you have a class
class A
{
int value;
p s v m(String args[]){
A obj1= new A();
abj1.value = 10;
A obj2= new A();
abj2.value = 10;
// now if try to compare these two classes
sop(obj1.equals(obj2)) // Result will be always false
} //unless you override
the equals method
boolean equals(A a){
if (a.value == this.value)
return true;
else
return false;
}//Placing this method in the above class will override
the equals method and you will be able to compare
actually the properties of those two object not
their references.
| Is This Answer Correct ? | 7 Yes | 0 No |
Post New Answer View All Answers
What are the differences between include directive and include action?
I want to re-reach and use an object once it has been garbage collected. Define how it’s possible?
What are the Static and Dynamic Variables? Differentiate them.
What is a java lambda expression?
Is string a class?
Explain hashset and its features?
what is the major difference between linkedlist and arraylist in java?
what is the difference between future and callable interface in java?
What are internal variables?
What should I import for arraylist in java?
Can you override private or static method in java?
What is the similarity between dynamic binding and linking?
Is break statement can be used as labels in java?
What is a method vs function?
What is the declaration statement?