why the equals method can be override?when we override the
equals method?
Answer Posted / ganesh
We need to override the equals method when we write our own class and we want the instance of that class to be part of some collection.
When you place the object in collection and make the manipulation on them, say sorting/searching etc.
The comparison of the object has to be done by the alogorithm.
The Algorithm uses the equals method to check for equality of two objects. Otherwise it wont be having any means to check for equality.
class Student
{
private String serialNo;
private String name;
}
two student objects with the same name cannot be the same students, but two student objects with the same serial number should the same object.
search(Student st,List a)
{
Collections.binarySearch(a,st);
}
when you want to put the objects in the hashMap, then overriding the cashCode is necessary. HashMap calls the hashCode method on the object to insert into HashMap.
| Is This Answer Correct ? | 4 Yes | 0 No |
Post New Answer View All Answers
Is static variable stored in heap?
What is polymorphism java example?
What is difference between calling start() and run() method of thread?
Does chrome use java?
Where is java located?
What are java packages? What's the significance of packages?
Can we declare main () method as non static?
Can an interface extend another interface?
Is multiple inheritance supported by java?
Can we override constructors?
What is difference between == and === in js?
Is there any sort function in java?
Explain about the interpreter in java?
What is the difference between break and continue statements?
What is the difference between a static and a non-static inner class in java programming?