This is an odd implementation of the equals
method.
It will take object o
and attempt to cast it to an object of type Dog
. It will then compare the size of that dog to the size of this
dog.
This is a problematic method since if we were to pass in something that cannot be cast to a dog(a socket, for example) it would throw a ClassCastException, which is a big no-no with equals(
. It must return false if the objects can't be cast for comparison.
I would rewrite it as follows:
boolean equals(Object o){
if(o==null) return false;
if(this==o) return true;
if(!o instanceOf Dog) return false;
return this.size==((Dog) o).size;
}