How to override a equals() method and what is the use?
Answer / 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 |
What is the difference between form & report?
What is google full form?
Which of the following can be referenced by a variable? A. The instance variables of a class only B. The methods of a class only C. The instance variables and methods of a class
how many types of Inheritance?
how to make the double-tone class ? as we have singletone class..?
Define the term string pool?
What is the purpose of void?
What is the use of math abs in java?
What is the main function in java?
What is deadlock and how to avoid this?
What about method local inner classes or local inner classes in java?
What is a protected class in java?