-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCovariantCompareTo.qhelp
More file actions
45 lines (32 loc) · 1.59 KB
/
CovariantCompareTo.qhelp
File metadata and controls
45 lines (32 loc) · 1.59 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
42
43
44
45
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Classes that implement <code>Comparable<T></code> and define
a <code>compareTo</code> method whose parameter type is not <code>T</code>
<i>overload</i> the <code>compareTo</code> method instead of <i>overriding</i> it. This may not be intended.
</p>
</overview>
<example>
<p>In the following example, the call to <code>compareTo</code> on line 17 calls the method
defined in class <code>Super</code>, instead of the method defined in class <code>Sub</code>, because
the type of <code>a</code> and <code>b</code> is <code>Super</code>. This may not be the method that
the programmer intended.</p>
<sample src="CovariantCompareTo.java" />
</example>
<recommendation>
<p>To <i>override</i> the <code>Comparable<T>.compareTo</code> method,
the parameter of <code>compareTo</code> must have type <code>T</code>.
</p>
<p>In the example above, this means that the type of the parameter of <code>Sub.compareTo</code> should be changed
to <code>Super</code>.</p>
</recommendation>
<references>
<li>J. Bloch, <em>Effective Java (second edition)</em>, Item 12. 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>
</references>
</qhelp>