The class Object defines an equals() method to compare objects.
Specify the advantages and disadvantages of using this method and suggest an alternative for equality
The equal method of the Java class is used to compare the equality of two objects. The main advantage of the equal method is that it is easy to use. This is possible especially if the objects being compared belong to the superclass.
if no inheritance is used.
Its main disadvantage is that with the use of inheritance, the programmer needs to override the equal method. This necessitates the need for additional code in the program.
Another disadvantage of using equals is that the program will not throw any exceptions if the equals() and hashCode() contract is violated
An alternative to using the equals method of Object class is usually to write your own code to perform the same.
Comments
Leave a comment