Example from learning material:
public class SimpleDate {
private int day;
private int month;
private int year;
public SimpleDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return this.day;
}
public int getMonth() {
return this.month;
}
public int getYear() {
return this.year;
}
public boolean equals(Object compared) {
// if the variables are located in the same position, they are equal
if (this == compared) {
return true;
}
// if the type of the compared object is not SimpleDate, the objects are not equal
if (!(compared instanceof SimpleDate)) {
return false;
}
// convert the Object type compared object
// into a SimpleDate type object called comparedSimpleDate
SimpleDate comparedSimpleDate = (SimpleDate) compared;
// if the values of the object variables are the same, the objects are equal
if (this.day == comparedSimpleDate.day &&
this.month == comparedSimpleDate.month &&
this.year == comparedSimpleDate.year) {
return true;
}
// otherwise the objects are not equal
return false;
}
}
I need help with this part of code and what follows it : if (!(compared instanceof SimpleDate)) {
return false;
}
So if an object beeng compared with is not of type SimpleDate, return false. And then the next line of code is converting compared object into SimpleDate type object. But that line of code can not be executed if compared object type in not of SimpleDate type according to the condition above, which returns false (meaning it stops there). And if that condition is false, which means compared object is of SimpleDate type, what is the point of converting it to the SimpleDate type again? Am I missing something here