Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can understand the confusion. I have been there before, so I'll share how I keep the basic differences straight...</p> <p><code>virtual</code> vs. <code>abstract</code>:</p> <ul> <li><p>If a class method (or property) is marked <code>virtual</code>, then it <strong>may be</strong> overridden using the <code>override</code> keyword, if you choose to <em>inherit</em> (aka <em>derive</em>) from that class. <BR><BR>The <code>virtual</code> keyword is intended to evoke the idea that the method may or may not be the actual method called. Therefore, I always think of <code>virtual</code> members as <em>default implementations</em>, meaning that it represents functionality that can be generalized, such as an <code>Eat()</code> method on a <code>Human</code> class, which might involve eating with one's hands. However, a <code>ChineseHuman</code> class might override the <em>default implementation</em> of <code>Eat()</code>, in order to allow for an implementation that uses chop sticks instead. Finally, because virtual methods and properties are <em>default implementations</em>, the class that defines that member must provide a complete implementation of the method or property. All <code>Human</code> objects must <em>know how to</em> <code>Eat()</code>. <BR><BR>An object-oriented way of thinking might declare that <code>virtual</code> members represent <em>instincts</em>. To <code>Eat()</code> is an <em>instinct</em> of a <code>Human</code> class object. A <code>ChineseHuman</code> may <em>learn</em> to <code>Eat()</code> with chop sticks.</p></li> <li><p>If a class method (or property) is marked <code>abstract</code>, then it <strong>must be</strong> overridden using the <code>override</code> keyword, if you choose to <em>inherit</em> from that class.<BR><BR>The <code>abstract</code> keyword is intended to evoke the idea that the class only <em>supports</em> the capability represented by the member, and that there is not any common logic that can be generalized for that feature. In other words, <code>abstract</code> members are only <em>conceptual</em>, and therefore they lack an implementation. It is a little confusing that C# asks us to <code>override</code> abstract members when we implement an inheritance relationship, but in this case it really means that we are overriding the empty concept with a concrete implementation. An example of an <code>abstract</code> member of a <code>Human</code> class might be <code>Speak()</code>. There would not be a common way of speaking for all <code>Human</code> objects, nor is it instinctual, because it requires language to express. Note: Some might argue that <code>Speak()</code> belongs on an <code>interface</code> instead.<BR><BR>An object-oriented way of thinking might declare that <code>abstract</code> members represent behavior (methods) <em>to be learned</em> and knowledge or beliefs (properties) <em>to be acquired</em>. To <code>Speak()</code> is a <em>learned</em> behavior of a <code>Human</code> class object. A <code>ChineseHuman</code> may learn to <code>Speak()</code> differently than an <code>EnglishHuman</code> and neither knows how to <code>Speak()</code> just because they are both <code>Human</code>.</p></li> </ul> <p>Nuances:</p> <ul> <li><code>virtual</code> methods do NOT need to be overridden.</li> <li>There is no such thing as a <code>virtual</code> class.</li> <li><code>abstract</code> members can only appear on <code>abstract</code> classes. In the above examples, having an <code>abstract</code> method on the <code>Human</code> class implies that <code>Human</code> is an <code>abstract</code> class, and that, therefore, a <code>Human</code> cannot be instantiated using the phrase <code>var baby = new Human();</code>. Instead, the <code>BabyHuman</code> class should inherit from <code>Human</code>, and it should be instantiated as <code>var baby = new BabyHuman();</code>. Because a <code>BabyHuman()</code> is a <code>Human</code> and <code>EnglishHuman</code> and <code>ChineseHuman</code> both also inherit from <code>Human</code>, <code>EnglishHuman</code> could inherit from <code>BabyHuman</code> instead of <code>Human</code>. Being <code>Human</code> is <code>abstract</code> because we are all something more than simply <code>Human</code>. </li> <li><code>abstract</code> members cannot be hidden, only their <code>override</code> implementations may be (further up the inheritance chain). For example, <code>BabyHuman</code> must implement the <code>abstract</code> <code>Speak()</code> method as an <code>override</code>. If <code>EnglishHuman</code> inherits from <code>BabyHuman</code>, it may then hide the <code>BabyHuman</code> implementation of <code>Speak()</code> with its own implementation by using the <code>new</code> keyword (see "Method Hiding in C#" reference below).</li> <li><code>abstract</code> classes can have <code>virtual</code> members. That's a main distinction between an <code>interface</code> and an <code>abstract</code> class. In that sense, an <code>abstract</code> class can define both a contract and a template of the class' behavior, whereas an <code>interface</code> only defines a contract.</li> </ul> <p>Code Reference:<BR></p> <ul> <li>Abstract Class versus Interface<BR> By Rahman Mahmoodi<BR> <a href="http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx" rel="noreferrer">http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx</a></li> <li>Method Hiding in C#<BR> By Chetan Kudalkar<BR> <a href="http://www.codeproject.com/KB/cs/cs_methodhiding.aspx" rel="noreferrer">http://www.codeproject.com/KB/cs/cs_methodhiding.aspx</a></li> </ul>
    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.
    3. 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