+ 1

Equal() help!!

"equalexample" is the code from sololearn."myequal"is edited by me.In their example,they override equal method to compare data.but in my code,i just compare a1.name with a2.name.isnt that much simpler?what is the use of applying equal method? https://code.sololearn.com/c4N53fpRFu70/?ref=app https://code.sololearn.com/cqr3J0i36igW/?ref=app

1st Nov 2017, 3:30 PM
oyl
5 odpowiedzi
+ 3
Explanation of everything P1. You are comparing the Strings, not the Animals. So, you are not comparing equality of Animals. To show this, add more attributes to the Animals and create more Animal Objects. Modify both methods so that they compare each attribute. You will manually have to compare every attribute. Notice the amount of code required to do that in order to compare each attribute. You have to do this every single time you want to compare for equality. You may want to turn it into a method so that it doesn't take up so much code. Now, that method must accept 2 Animals as arguments. So, you have something like: static boolean isEqual(Animal a, Animal b{ return a.name.equals(b.name); /* I'll write the code as if name is the only attribute. You can pretend there is more. */ }
1st Nov 2017, 4:17 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Explanation of everything P2. One issue here is that If you ever want to compare an Object you would first need to check if it is an Animal. Then, you would call that method and downcast each Animal. If I move this method to the Animal class then I no longer nees to check if both Objects are an instance of an Animal. I still need to check one, but now we can do that in the equals() method instead of when we call the method. The method should no longer be static since we want to require an instance of Animal in order to even call the method Now, the method looks like this: boolean equals(Animal a){ return this.name.equals(a.name); } // Inside the Animal class Lets say for example we want to have an Array that stores all Objects in our world for our game. That way, we have instant access to all of them and this is ok since there will only ever be 10 of these Objects. Object[] world = new Object[10]; This world will not only contain Animals. Now, if we have an instance of an Animal and want to compare equality to an Object, what do we need to do? Much like before, everytime we call the equals method we must first check if the Object is an instance of an Animal so we can downcast it to call this method. We don't want to be forced to right more code everytime we call a method, the method should handle everything for me. So, we decide to just allow the equals method to take an Object instead. Keep in mind this now has the same signature to the method in the Object class, which all classes by default inherit. This means we are Overriding that method.
1st Nov 2017, 4:17 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Explanation of everything P3. One amazing thing about this is that we can now call the equals method with any Object instance and still get the right result whether or not it is an Animal. Maintaining this now becomes a piece of cake. One issue now is that the attribute 'name' may not be an attribute of the Object. So, we do need to check to see if the Object is an instance of an Animal. This is okay since it will all be handled in the equals method for us, we do not need to keep checking whenever calling the method. There is also a chance for the Object to be null, which would clearly cause a NullReferenceException, so lets handle that by quickly checking if the Object is null. The method now looks something lke this: @Override boolean equals(Object a){ if(a == null) return false; if(getClass() != a.getClass()) return false; return this.name.equals((Animal)a.name); } We can also easily check to see if this Object already is the Object in the parameters by using ==. If the Obejcts are litteraly the same, they are equal. We do not want a NullReferenceException if the name of 'this' Object is null. So, we first check for that to make sure it is not: Our code now looks like the original: @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Animal other = (Animal) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } Note* getClass() can be modified to use the instanceof keyword instead. However, Sololearn never really taught about that keyword.
1st Nov 2017, 4:17 PM
Rrestoring faith
Rrestoring faith - avatar
0
could you write the whole code in code playground so that i could understand it better.please thanks
1st Nov 2017, 6:03 PM
oyl
0
isnt the use of overriding equal method is to compare data between 2 objects?what is wrong with comparing the strings only?
2nd Nov 2017, 6:53 AM
oyl