I'm learning Java language and I have one question for you.
For example: I have a class Employee like this:
public class Employee {
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
So I should write method equals() like:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return id == employee.id && Objects.equals(name, employee.name);
}
or
@Override
public boolean equals(Object o) {
if (super.equals(o)) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return id == employee.id && Objects.equals(name, employee.name);
}
My book says:
if you redefine the equals method in a subclass, use super.equals(other).
but all classes extends Object class so which version of this method is better? I think that the first option will be faster (CPU). And the first method is the most popular, but why?