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 is ellipsis in java?
What are serialization and deserialization?
What do negative exponents mean?
What is the static variable?
Which class is the superclass for every class in java programming?
What is static keyword in java?
Tell me how many ways are there to initialise an integer with a constant.
what are different ways in which a thread can enter the waiting state? : Java thread
Where can I find jdk in my computer?
What are the two ways of implementing multi-threading in java?
Why main function is static?
What is member in java?
What is difference between pointer and reference?
Why Set interface contains unique elements, what internally implemented for this so that it contains unique elements?
What are untrusted applets?