Does the equals method work with objects? If so, how?
Asked Answered
G

3

5

I have a program that is zoo and in the zoo there are branched subgroups of animals that are reptiles. When I do an equals method the main program compiles and it runs. I'm confused how does java know to use the equals method if I'm comparing objects and not specifically int or String?

public class Zoo {

    public static void main(String[]args) {
        Animal a=new Animal("Bob");
        Reptile komodo= new Reptile("Snakey");
        komodo.bask();
        a.size=3;
        komodo.size=5;
        System.out.println(a);
        System.out.println(komodo);
        Turtle t= new Turtle("Slowy");
        t.hide();
        t.size=6;
        t.numlegs=4;
        System.out.println(t);
        System.out.println(t.equals(komodo));
    }
}

public class Animal {
    public String name;

    public boolean equals(Animal other) {
        return other.size==this.size;
    }

    public Animal(String s) {
        name=s;
    }
    public void setName(String n) {
        this.name=n;
    }
    public void eat(String meal) {
        System.out.println("chump chump yummy "+meal);
    }
    public int size;

    public String toString() {
        return "I am "+name+" and I'm "+size+" cm long";
    }

}

public class Reptile extends Animal {

    public Reptile(String n) {
        super(n);
        numlegs=0;
    }
    public Reptile(String n, int l) {
        super(n);
        numlegs=l;
    }

    public void bask() {
        System.out.println("basking...");
    }
    public String toString() {
        return super.toString()+numlegs+" legs";
    }

    public int numlegs;
}
public class Turtle extends Reptile {

    public Turtle(String n) {
        super (n,4);
        shellColor="Brown";
    }
    public void hide() {
        System.out.println("you cant see me");
    }
    public String toString() {
        return super.toString()+" and my shell is"+ shellColor;
    }
    public String shellColor;

    public void bask() {
        super.bask();
        System.out.println("turtle is basking...");
    }

}
Gallipot answered 9/7, 2013 at 14:46 Comment(0)
W
11

You're not overriding the Object#equals method, but overloading it. In your method declaration you use Animal type instead of Object:

public boolean equals(Animal other)

A good overriding of the method would be using the instanceof operator. Showing an example:

@Override
public boolean equals(Object other) {
    if(other instanceof Animal) {
        Animal otherAnimal = (Animal)other;
        //comparison logic...
    }
    return false;
}

More info on the subject:

Westward answered 9/7, 2013 at 14:49 Comment(0)
R
2

For your question on how java knows how to compare objects, you need to override the equals method

 public boolean equals(Object other){
    // return true or false based on your logic

  }

While comparing, equals method is used. You can have a look at this good tutorial which explains the significance of the equals method.

http://www.thejavageek.com/2013/06/26/what-is-the-significance-of-equals-method-in-java/

Also, only overriding equals is not enough if you are using objects into collections those use hashing. You will find a good tutorial at

http://www.thejavageek.com/2013/06/28/significance-of-equals-and-hashcode/

Ruelu answered 9/7, 2013 at 14:50 Comment(5)
Object do not have member size.Januaryjanuisz
+1 for mentioning overridding hashing, i'd say though that the hash should always be overridden if the equals is overridden otherwise the contract is broken. A good IDE will generate the new hash for youQuotable
@RichardTingle yes sir I have mentioned all those points in the tutorial I've provided. It will help me improve if you have a look at it and provide feedback also. That will be really appreciatedRuelu
@RichardTingle the IDE would only call super.hashCode() so no matter how good the IDE is, the auto generated implementation of hashCode is not right =\Westward
@LuiggiMendoza My IDE (netbeans) allows you to click the warning message about not overriding the hashcode and select "generate hash code", you then select the fields you want to use for your hash and it autogenerates itQuotable
S
0

Every class inherits the Object class silently. And the Object class has a equals method. So if any class doesn't override the equals method then it will use the default implementation of Object.equals.

From the doc

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

From the source code of Object.equals

public boolean equals(Object obj) {
    return (this == obj);
}

So If any object doesn't have it's own implementation of equals then the equals method will simply check if the object reference is same or not.

So get a desired result from equals you need to implement by your own as alread suggested in other answer

Slenderize answered 9/7, 2013 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.