Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is operator overloading. Is it is supported in java?

1092


Which is dependent variable?

954


What access modifiers can be used for variables?

1209


How do you calculate roots in java?

1159


Who is founder of java?

1040


Does a class inherit the constructors of its superclass in java programming?

1212


What happens if main method is not static?

1050


What is the difference between static binding and dynamic binding?

1198


Why vector class is used?

1034


What is an inner class in java?

1023


What is the difference between math floor and math round?

1109


What is parameters example?

1091


Which java collection does not allow null?

1213


What are the access modifiers available in java?

1183


Explain different ways of creating a thread. Which one would you prefer and why?

1102