How do I override an object method in JavaScript?

Is my understanding of overriding equals() method correct?

  • 1) When we override equals() method, we send object as argument. a)What would happen if we send a primitive? b) What would happen if we send something other than object? 2) Should we use instanceof and casting the object argument into corresponding class type and then do a == check in the equals() method? Example: if( (o instanceof   Moof)   && (  ( (Moof) o).getMoofValue() == this.moofValue) ){ 3) Why are we using == inside equals() method? Won't they just compare if two reference variable refer to a single object on heap rather than finding if contents of two objects are equal? 4) What would happen if I try to override as well as overload equals() method?

  • Answer:

Toby Thain at Quora Visit the source

Was this solution helpful to you?

Other answers

Interface is one of the important features of Java, it makes possible the reuse of classes even if we define a new class later. (The designer doesn't know your class, but they just match!) The equals() method is not only part of the Object but also part of other interfaces such as http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html and http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html. The equals method is commonly used in http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html where from time to time the collection needs to know whether there is a certain object in the collection.     Here's the point. When we make up our manual classes, we do not always want to compare them in an "exactly same" manner (that's already possible by using the == operator!)  For instance, when we use the http://docs.oracle.com/javase/8/docs/api/java/lang/String.html, we expect str1.equals(str2) to be true when both of them are "sometxt", but str1==str2 is not necessarily true. [1] As a new class example, you may define a Complexnumber Class and specify its equals() function to be true only when the two real parts and imaginary parts are same. The == operator just means less when we actually want to "compare" the two Complexnumbers.     To sum up, we override the equals() method since when defining our own classes, it is used by both the class itself and other classes. So there is no use when you overload it for no other class recognizes the overloaded version. [1] String may not be a good example since there are some tricks for efficiency, but that's not the topic today so I'm not going to go further on this.

Teng-Pao Yu

1.Override of equals() method :               If u want content comparison of two objects instead of reference comparison you should override equals() method of object class. Which is by default available every class because of Object class act as parent class to all the classes.          (a)if u send primitive as argument:                            java having auto boxing capability due to that if send any primitive that would be converted into corresponding object.                For example:   equals(int 5)  becomes equals(new Integer(5));            (b) if u send something other than object:                           for example if u send null you will get NullPointerException (or) except null and object if u pass  anything u will get CompileTime Error 2. Should we use instanceof and casting the object argument into     corresponding class type and then do a == check in the            equals() method?                                   Yes, You should do that. It's the appropriate implementation of     equals() method. To avoid ClassCastException. 3. Why are we using == inside equals() method?                 1. if both the references representing same object their content should be same.                2. if both the reference representing different objects their content might be same or might not be. So in second condition content comparison is mandatory in that situation if u have primitive values in your object == is enough to check both values are same or not.  That's why we are using == in equals() method. 4. override as well as overload equals() method:             nothing will happen they will work as usual.             1.if u call equals() method with no arguments then overridden method will be called.             2. if u call equals() method with arguments then overloaded method will be called.

Bhargav Reddy Dagumati

You can read my answer for the same question

Alok Singh

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.