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
What is meant by distributed application? Why are we using that in our application?
What is treeset in java?
there are N number of matchboxes numbered 1...N.each matchbox contain various number of stick.Two player can alternatevely pick some amount of stick from the higest stick containing box . The player is condidered win if there is no stick after his move.Find the final move so that the move player win. Note:In case the number of stick is equal ,pick the stick from the higest numbered box.
What is a java developer salary?
What is the basic difference between string and stringbuffer object?
What data type is a string?
Is singleton set an interval?
Can you inherit a constructor java?
What is java Applet?
Can a class have multiple constructors?
Where is java located?
What is equlas() and hashcode() contract in java? Where does it used?
What is methodological theory?
What is constructor and its types?
What is user defined exception in Java?