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
How are java objects passed to a method and what are native methods?
what is object-oriented programming in java?
Is main is a keyword?
what is the purpose of using rmisecuritymanager in rmi?
What is numeric function?
What is the different between get and post?
Similarity and difference between static block and static method ?
If a variable is declared as private, where may the variable be accessed?
What is a package in java? List down various advantages of packages.
Can we create constructor in abstract class ?
What is the purpose of using break in each case of switch statement?
What is use of set in java?
What are constants and how to create constants in java?
How do you convert bytes to character in java?
Which method you will use to create a new file to store some log data. Each time a new log entry is necessary, write string to the file in java ?