Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting in equals method

I have a question about overriding the equals method in Java. In my book, I have the following example:

public class Dog{
     private String name;
     private int age;

public boolean equals(Object obj) {
    if(!(obj instanceof Dog)) return false;
    Dog other = (Dog) obj;                    ---> confused here
    if(this.name.equals(other.name) && (this.age == other.age) {
     return true;
    }
    else {
     return false;
    }
  }
}

I don't understand why why have to cast the reference to the Dog reference. If that reference is not of type Dog we return false. Why all the hassle with casting it ?

like image 990
user2326847 Avatar asked May 11 '13 13:05

user2326847


People also ask

What is an equals method?

Equals method checks to make sure that the obj argument is not null and that it references an instance of the same type as this object. If either check fails, the method returns false . The Point. Equals method calls the GetType method to determine whether the run-time types of the two objects are identical.

What is the function of the equals () method?

The equals() method compares two strings, and returns true if the strings are equal, and false if not.

How do you use char in equals method?

But char is not an Object type in Java, it is a primitive type, it does not have any method or properties, so to check equality they can just use the == equals operator. Show activity on this post. You can also use a comparator if you want.

Which class is an equals method?

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 ).


2 Answers

Because you are defining your own parameters for equality, you have to make sure they are the same class. That is, unless you're comparing them the == way, then you need to compare some value inside the objects. To compare values inside the objects, they need to be the same type!

For example, let's say you have two Dogs.

Dog dog1 = new Dog("Fido");
Dog dog2 = new Dog("Rover");

If you want to test if they have the same name, as I'm sure you know, you can't use:

if(dog1 == dog2)

So you override the equals method. However, because you're overriding it, it has to have the same method signature. A method signature is defined by the name of the method, and the number and type of it's parameters. Which means if you wish to override it, it needs to have a parameter of type Object. Hence:

if(dog1.equals(dog2))

The reason you need to cast it to use whatever method you're using to get the name value from the dog, and compare those values.

A note on your class design

The convention in object oriented programming, and certainly in Java, is to have Accessor and Mutator methods to get and change variables in a class. That is:

dog1.name; ----> dog1.getName();

where getName() looks like:

public String getName()
{
    return name;
}
like image 98
christopher Avatar answered Oct 21 '22 16:10

christopher


The declared type of obj is Object, so you must cast it to tell the compiler that it is a Dog.

Although logically it can't be anything else at that point in the code, the compiler doesn't know anything about logic - it only knows about the type.

like image 24
Bohemian Avatar answered Oct 21 '22 14:10

Bohemian