Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I envision your code now is that you have an abstract base class containing a method (<code>GetFruit</code>) that returns an interface that I'll call <code>IFruit</code>. In a derived class, you want to be able to return (from this same <code>GetFruit</code> method) an <code>Apple</code> object that implements the <code>IFruit</code> interface.</p> <p>However, as convenient as that might be, <strong>the Common Language Specification (CLS) does not allow compliant languages to support <em>return type covariance</em></strong>. This includes all of the .NET languages, like VB.NET and C#. There is a lot of misinformation that exists on exactly why this limitation arises and what exactly is responsible for this limitation, but the simple answer is that you cannot narrow the return type on a method in a derived class to a more specific derivation.</p> <p>That means that even though <code>Apple</code> implements <code>IFruit</code>, you cannot type the method in a derived class to return <code>Apple</code>. The overridden methods must have the same return value signature as that of their base. <strong>So, instead, each method must return an object of the same type</strong> (<code>IFruit</code>) <strong>for all classes that derive from your base class.</strong> Simply changing the return value of the method in your derived class to be the same type as the method in the base class will solve your problem.</p> <p>You mention the possibility of making the base class generic, and while this would probably work, it seems to introduce unnecessary complexity. Using my contrived example above, since an <code>Apple</code> fully implements the <code>IFruit</code> interface, you should get the full benefit of type checking and even Intellisense by simply returning <code>IFruit</code> from your derived classes.</p> <p><strong>EDIT:</strong> If you're open to something a little bit hack-ish, you can always re-declare the method in your derived class (by marking it as <code>new</code> or <code>Shadows</code>). This will allow you to return whatever type of object that you want, regardless of the signature of the method in the base class. However, remember that there is no way to re-declare and override a method at the same time, so you're not truly polymorphic.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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