|
5 | 5 |
|
6 | 6 |
|
7 | 7 | <overview> |
8 | | -<p> There are one (or more) legal parameters for an overridden method that are |
9 | | -not legal for an overriding method. This will cause an error when the overriding |
10 | | -method is called with a number of parameters that is legal for the overridden method. |
11 | | -This violates the Liskov substitution principle. |
| 8 | +<p> When the signature of a method of a base class and a method of a subclass that overrides it don't match, a call to the base class method |
| 9 | +may not be a valid call to the subclass method, and thus raise an exception if an instance of the subclass is passed instead. |
| 10 | +If following the Liskov Substitution Principle, in which an instance of a subclass should be usable in every context as though it were a an |
| 11 | +instance of the base class, this behavior breaks the principle. |
12 | 12 | </p> |
13 | 13 |
|
14 | 14 | </overview> |
15 | 15 | <recommendation> |
16 | 16 |
|
17 | | -<p>Ensure that the overriding method accepts all the parameters that are legal for |
18 | | -overridden method.</p> |
| 17 | +<p>Ensure that the overriding method in the subclass accepts the same parameters as the base method. </p> |
19 | 18 |
|
20 | 19 | </recommendation> |
21 | 20 | <example> |
22 | | -<p>In this example there is a mismatch between the legal parameters for the base |
23 | | -class method <code>(self, source, filename, symbol)</code> and the extension method |
24 | | -<code>(self, source)</code>. The extension method can be used to override the base |
25 | | -method as long as values are not specified for the <code>filename</code> and |
26 | | -<code>symbol</code> parameters. If the extension method was passed the additional |
27 | | -parameters accepted by the base method then an error would occur.</p> |
| 21 | +<p>In the following example, <code>Base.runsource</code> takes an optional <code>filename</code> argument. However, the overriding method |
| 22 | +<code>Sub.runsource</code> does not. This means the <code>run</code> function will fail if passed an instance of <code>Sub</code>. |
| 23 | +</p> |
28 | 24 |
|
29 | 25 | <sample src="SignatureOverriddenMethod.py" /> |
30 | 26 |
|
31 | | -<p>The extension method should be updated to support the <code>filename</code> and |
32 | | -<code>symbol</code> parameters supported by the overridden method.</p> |
33 | | - |
34 | 27 | </example> |
35 | 28 | <references> |
36 | 29 |
|
|
0 commit comments