boolean equals (Course c);The fully polymorphic equals would have the signature:
boolean equals (Object c);Several students noted that this causes a problem when Course or Student objects are used in container classes such ListImpl. The problem arises as follows:
...which compares the given o to some local Object m. It uses a method of o which must have the signature equals(Object x), because that's how it's called. Even though o sometimes refers to an instance of C during execution, it is not generally true that o refers to something with a method equals(C x) - so Java doesn't arrange to call such a method ever.
if (o. equals (m))
...
If you must define one, because you have an interface which requires it, then define the fully polymorphic one to use the nonpolymorphic one:
boolean equals (Course c) // new method, for interface
{
return this. title. equals (c. title);
}
boolean equals (Object c) // override inherited signature
{
if (c instanceof Course)
return equals ((Course) c);
else
return false; // not the same type, can't be equal
}