Reputation: 21
I'm confused to what Vehicle other = (Vehicle)obj;
does. Does it create a variable other
and copies obj
into it?
@Override
public boolean equals (Object obj) {
if (this == obj) return true;
if (!(obj instanceof Vehicle)) return false;
Vehicle other = (Vehicle)obj;
return ( type.equals(other.type)
&& size == other.size
&& uitstoot == other.uitstoot
);
}
Upvotes: 1
Views: 92
Reputation: 722
I think you are correct.
First it transform the Object obj to Vehicle class ( we already checked the obj could be transformed to Vehicle class, otherwise code couldn't go to here )
Second give the copy to the other variable in order to protect original data.
Upvotes: 0
Reputation: 26185
It does two things. It declares other
to be a Vehicle
reference, and it initializes it as a pointer to the object that is referenced by obj
. The cast will never cause a runtime exception, because that object has already been determined to be a Vehicle
.
Upvotes: 0
Reputation: 15683
First you check if the obj
is instance of Vehicle
if (!(obj instanceof Vehicle)) return false;
If it is then it is cast to Vehicle
class - i.e from that point onwards, it will be interpreted as Vehicle
instance
Vehicle other = (Vehicle)obj;
Upvotes: 2