-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCovariantEquals.qhelp
More file actions
41 lines (28 loc) · 1.63 KB
/
CovariantEquals.qhelp
File metadata and controls
41 lines (28 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Classes that define an <code>equals</code> method whose parameter type is not <code>Object</code>
<i>overload</i> the <code>Object.equals</code> method instead of <i>overriding</i> it. This may not be intended.</p>
</overview>
<recommendation>
<p>To <i>override</i> the <code>Object.equals</code> method,
the parameter of the <code>equals</code> method must have type <code>Object</code>.</p>
</recommendation>
<example>
<p>In the following example, the definition of class <code>BadPoint</code> does not override the <code>Object.equals</code> method.
This means that <code>p.equals(q)</code> resolves to the default definition of <code>Object.equals</code> and
returns <code>false</code>. Class <code>GoodPoint</code> correctly overrides <code>Object.equals</code>,
so that <code>r.equals(s)</code> returns <code>true</code>.</p>
<sample src="CovariantEquals.java" />
</example>
<references>
<li>J. Bloch, <em>Effective Java (second edition)</em>, Item 8. Addison-Wesley, 2008.</li>
<li>Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.4.8.1">Overriding (by Instance Methods)</a>,
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.4.9">Overloading</a>.</li>
<li>The Java Tutorials: <a href="https://docs.oracle.com/javase/tutorial/java/IandI/override.html">Overriding and Hiding Methods</a>.</li>
<!-- A related issue is examined by the query <a href="MissingInstanceOfInEquals.html">Equals Method Does Not Inspect Argument Type</a>. -->
</references>
</qhelp>