Home > Net >  Overriding Equals in Java - confused about implementation
Overriding Equals in Java - confused about implementation

Time:04-01

I've been doing the MOOC Java programming course and they typically override equals like this:

  1. If addresses of the 2 objects are the same = return true.
  2. If compared object is not an instance of the same class as the first object = return false.
  3. Convert the object to the same type as the one being compared to
  4. Compare the variables of each object - returns true or false

I'm just a bit confused about step 2 and 3.

  • Step 2 checks if the 2nd object is an instance of the same type as the one its compared to...

If it isn't, the method ignores the rest of the code and returns false

If it is, we convert it to that Class. So at this point i'm confused about why we need to convert an object that is already an instance of the same class - to that class? If Java recognises it is of the same type, why do you need to convert it?

*Example below:

public boolean equals(Object comparedObject) { 
// if the variables are located in the same place, they're the same 
  if (this == comparedObject) { 
    return true; 
  }

// if comparedObject is not of type Book, the objects aren't the same **   
  if (!(comparedObject instanceof Book)) { 
    return false; 
  }
// convert the object to a Book object
Book comparedBook = (Book) comparedObject;**

// if the instance variables of the objects are the same, so are the objects
  if (this.name.equals(comparedBook.name) &&
    this.published == comparedBook.published &&
    this.content.equals(comparedBook.content)) {
    return true;
}

// otherwise, the objects aren't the same
return false;
}

CodePudding user response:

Because you’ve only compared the dynamic type; the static type of comparedObject is unchanged: it’s Object, and you cannot access its Book members (think about it: at this point you know that the dynamic type of comparedObject is Book; but the Java compiler still sees it with its declared type). To make the compiler understand that the object type is Book, you first need to create an object whose static type is Book, and to do that you need a cast.

The static type information is what the Java compiler sees, and which is what the compiler uses to check whether your code is statically correct.

CodePudding user response:

Java is statically typed, so if the variable is declared as Object it will not let you access any fields or call any methods of Book. In this case, it is easily proven that the object can only be a Book, but the compiler does not make any attempt to prove it. (And, in a more complicated example, it couldn't.)

In the newest versions of Java, there is a shortcut you can take with the syntax:

if (comparedObject instanceof Book book) {
    // do whatever with book
    return this.title.equals(book.title) && this.author.equals(book.author);
}

But in earlier versions, you simply have to downcast the instance:

if (comparedObject instanceof Book) {
    Book book = (Book) comparedObject;
    return this.title.equals(book.title) && this.author.equals(book.author);
}
  • Related