How does the equals method work?

2

I stopped in an exercise of a book I'm reading and in this shows an example of overloading the equals method, I even understood the concept that it compares the reference between two objects, but in the method: public boolean equals(Object obj) things began to get a little confusing.

My questions are:

  • What this method returns equals : return (getConta() == ((ExemploContaEquals) obj).getConta());
  • Why does if of the main method compare only the last two attributes of instantiated objects? That is, it only compares the numbers 20 and 21 of the obj1 and obj2 objects respectively and the numbers 50 and 50 of the obj3 and obj4 objects. What's the difference then having two attributes?

    package modulo04.EqualsBackup;

    public class ExemploContaEquals {
    
        private int conta = 0;
    
        public ExemploContaEquals(int agencia, int conta){
            this.conta = conta;
    
        }
    
        public ExemploContaEquals(){
            this(0,0);
        }
    
    
    
        public int getConta(){
            return conta;
        }
    
    
        public boolean equals(Object obj){
    
            if(obj != null && obj instanceof ExemploContaEquals){
    
                return (getConta() == ((ExemploContaEquals) obj).getConta());
    
            }  else {
                return false;
    
            }
    
        }
    
    }
    
package modulo04.SobrecargaEquals;

public class ExemploContaEqualsPrincipal {

    public static void main(String[] args) {

        ExemploContaEquals obj1 = new ExemploContaEquals(10,20);
        ExemploContaEquals obj2 = new ExemploContaEquals(10,21);

        if(obj1.equals(obj2)){
            System.out.println("Contas iguais");

        } else {
            System.out.println("Conta diferentes");
        }

        ExemploContaEquals obj3 = new ExemploContaEquals(10, 50);
        ExemploContaEquals obj4 = new ExemploContaEquals(20,50);

        if(obj3.equals(obj4)){
            System.out.println("Contas iguais");

        } else {
            System.out.println("Contas difentes");
        }
    }

}
    
asked by anonymous 02.02.2016 / 02:59

2 answers

4

Well, come on.

About the following point:

  

What does this return of the equals method mean: return (getContact) == ((ExampleContaEquals) obj) .getContact ());

It is checking whether the current instance of the ExemploContaEquals class has an equivalence between them. (Seeing the conta attribute as the comparison parameter)

Still on your doubt, verification is done through casting, since all classes inherit from Object , allowing comparison to be possible as long as the object passed in the parameter is an instance of ExemploContaEquals .

And if there is equivalence between the two attributes of the objects used in the comparison, true will be returned as a result.

Already about the following doubt:

  

Why is the if of the main method compares only the last two attributes of the instantiated objects? or just compares the numbers 20 and 21 of the obj1 and obj2 objects respectively and the numbers 50 and 50 of the obj3 and obj4 objects. What is the difference then having two attributes?

In this case, as the method is being overwritten, it is up to the developer to stipulate the parameter that will be taken into consideration at the time of the actual comparison.

In this example, the author preferred to use the conta attribute as a parameter, but nothing prevents him from deciding to change that later to the agency verification, for example, or to include the two attributes as a comparison parameter. p>     

02.02.2016 / 03:25
4

Equals() is a method that comes from class Object and in its signature, expects an object of type Object as a parameter.

If the method signature was equals(ExemploContaEquals obj) , it would not be the same inherited method. Inheritance only occurs when the signature is exactly the same.

Then a cast is made to tell the compiler that you want to read this object as if it were an object of type ExemploContaEquals after all you know this is what you want. If you compare a ExemploContaEquals object with a Object , the second will not have access to ExemploContaEquals members even though they are there. The compiler prohibits access to members that are not of the specified type. When you say that it is of that type, there the compiler allows.

Of course, to do this without making a mistake you need to make sure that the "conversion" (there is no actual conversion) is possible and for this there is if previous ensuring that the object is of a compatible type. p>

The second question I would answer: "because the example has it done". Does not seem to have a special reason. Either way the method has to define what is being compared. It is a "business" decision that will be used, which is relevant to the comparison. I agree that the account alone without the agency is weird. But it's just a silly example, it will not run on a bank:)

    
02.02.2016 / 03:14