Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Whenever you override a method, you have to override with the same signature as in the base class (there are exceptions for covariance and contravariance, but those don't apply to your question, so I'll ignore them here).</p> <p>In <code>GeometricFigure</code>, you have the declaration</p> <pre><code>public abstract decimal ComputeArea(); </code></pre> <p>but in <code>Square</code> and <code>Triangle</code> you have the declaration</p> <pre><code>public override decimal ComputeArea(decimal _area) { // ... } </code></pre> <p>Let's say that some other class contained the following code:</p> <pre><code>GeometricFigure fig = new Triangle(10, 10); decimal area = fig.ComputeArea(); </code></pre> <p>Which <code>ComputeArea</code> would be called? <code>Triangle</code> doesn't define a <code>ComputeArea</code> with no arguments, and neither does <code>GeometricFigure</code>, so there is no valid <code>ComputeArea</code> to call. As a result, the language spec disallows this scenario by requiring that <code>override</code> only be placed on methods that actually override methods of the base class, with the same number and type of arguments. Since <code>ComputeArea(decimal)</code> doesn't override <code>ComputeArea()</code>, the compiler errors out and tells you that you must place the override keyword on a <code>ComputeArea()</code> definition in <code>Triangle</code>, and that you can't put the override keyword on <code>ComputeArea(decimal)</code>.</p> <p>That's not to say that you can't define a method <code>ComputeArea(decimal)</code> on <code>Triangle</code> and <code>Square</code>, but you can't declare it as overriding <code>ComputeArea()</code> in <code>GeometricFigure</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload