Friday, April 29, 2011

issue of virtual method in C#

Hello everyone,

In MSDN, it is mentioned,

http://msdn.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx

I am confused what does this item mean "A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier."?

(this is the 2nd differences between virtual and abstract)

thanks in advance, George

From stackoverflow
  • Can you explain what's confusing about it? Properties can be overridden like any other method.

    public class Base {
      public virtual int Prop1 { get { ... } set { ... } }
    }
    
    public class Derived : Base {
      public override int Prop1 { get { ... } set { ... } }
    
    George2 : My confusion is not overridden property, but why it is a differences between abstract and virtual -- ""A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier."? Please see the context of MSDN I quoted.
    Tor Haugen : Abstract members have no implementation, and so _must_ be overridden. Virtual members have an implementation, and _may_ be overridden. That's the difference.
    George2 : Understand, MSDN words are so fuzzy. :-)
    Coincoin : I don't think this is what the doc meant, or if it did, it's a very bad choice of words. Still, it's the best explanation. The writer was probably just past the Ballmer peak. Yay MSDN!
  • If you declare a method virtual in your base class you can override it in your derived class.

    Example

    class MyBaseClass
    {
       public virtual void MyOverridableMethod()
       {
              ...
       }
    
    }
    
    class MyDerivedClass : MyBaseClass
    {
       public override void MyOverridableMethod()
       {
             ...
       }
    }
    

    Notice the override modifier in MyDerivedClass.

    George2 : My confusion is what is the differences between abstract and virtual in the quoted words -- " virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier"?
  • The only difference between virtual and abstract, is that an abstract method or propery has no implementation in the class where it has been defined (the abstract class), and that it must be overriden in a subclass; whereas a virtual method or property has an implementation in the class where it has been defined, and so it is not mandatory to override it in a subclass.

    public abstract AbstractClass { // This class cannot be instantiated, since it is // abstract, and the class is abstract because // it has an abstract member

    public abstract MyProperty {get; set; }
    

    }

    In a class where you derive from AbstractClass (the above AbstractClass is just for explanation purposes; since it has no methods / properies that have an implementation, you could create an interface instead of an abstract class), you will have to provide an implementation of MyProperty. Otherwise, it won't compile. You do this by 'overriding' the MyProperty, you don't want to introduce a new member, but just provide an implementation for a property that has been defined previously.

    public class ConcreteClass : AbstractClass
    {
        public override MyProperty {
           get
           {
                return _someValue;
           }
           set
           {
                if( _someValue != value ) _someValue = value;
           }
    }
    
    George2 : So, you think MSDN means, for abstract base class, it is "must", but for non-abstract base class, it is "can" (not a must)?
    Frederik Gheysels : It is defenitly a must; try to inherit from an abstract class, and don't override an abstract method or property; the compiler will complain.
  • Okay, let's say you have a base class, and that that base class is, itself, derived from another class.

    public class Bar : Foo
    {
       virtual public int SomeProperty { get; set; }
    }
    

    What the virtual keyword means is that in a class derived from Bar, you can override SomeProperty to change its behavior:

    public class Baz : Bar
    {
       private int thisInt;
       override public int SomeProperty 
       {
          get { return thisInt; }
          set 
          {
             if(value < 0)
             {
                throw new ArgumentException("Value must be greater than or equal to zero.");
             }
             thisInt = 0;
          }
       }
    }
    

    Clarification: When an object of type Baz is used, its version of SomeProperty is invoked, unless the type is cast to Bar. If you define Baz's SomeProperty as virtual, classes derived from Baz can also override it (in fact, that may be required--can't recall right off the top of my head).

    Further Clarification: An abstract method has no implementation; when you add one to your class, you must also mark the class as abstract, and you cannot instantiate new instances of it, like this:

    MyAbstractType m = new MyAbstractType();
    

    Virtual members, on the other hand, can have an implementation (like SomeProperty, above), so you don't have to mark the class abstract, and you can instantiate them.

    George2 : Thanks Mike, you answered well but not what I am asking. My question is answered by Tor Haugen, and you can refer there. It is my fault that I do not make myself understood.
    Frederik Gheysels : It is not required to override virtual members.
    Mike Hofer : @George: I clarified the answer to accomodate your question. @Frederik: Note that I said that "you can override" a virtual member.
    Frederik Gheysels : @Mike: I was referring to this sentence of yours: In fact, that may be required--can't recall right off the top of my head
    Mike Hofer : @Frederik: Of course, you're right. I think I was trying to say something else. Way too early in the morning at that time, and I have no idea why I was trying to write that before enough coffee. :) Thanks for the clarification.
    Mike Hofer : Would someone care to explain the downvote? If I'm wrong, please point out where, and enlighten me. I'd rather learn than be left in the dark. Seriously.
  • I can understand the confusion. I have been there before, so I'll share how I keep the basic differences straight...

    virtual vs. abstract:

    • If a class method (or property) is marked virtual, then it may be overridden using the override keyword, if you choose to inherit (aka derive) from that class.

      The virtual keyword is intended to evoke the idea that the method may or may not be the actual method called. Therefore, I always think of virtual members as default implementations, meaning that it represents functionality that can be generalized, such as an Eat() method on a Human class, which might involve eating with one's hands. However, a ChineseHuman class might override the default implementation of Eat(), in order to allow for an implementation that uses chop sticks instead. Finally, because virtual methods and properties are default implementations, the class that defines that member must provide a complete implementation of the method or property. All Human objects must know how to Eat().

      An object-oriented way of thinking might declare that virtual members represent instincts. To Eat() is an instinct of a Human class object. A ChineseHuman may learn to Eat() with chop sticks.

    • If a class method (or property) is marked abstract, then it must be overridden using the override keyword, if you choose to inherit from that class.

      The abstract keyword is intended to evoke the idea that the class only supports the capability represented by the member, and that there is not any common logic that can be generalized for that feature. In other words, abstract members are only conceptual, and therefore they lack an implementation. It is a little confusing that C# asks us to override 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 abstract member of a Human class might be Speak(). There would not be a common way of speaking for all Human objects, nor is it instinctual, because it requires language to express. Note: Some might argue that Speak() belongs on an interface instead.

      An object-oriented way of thinking might declare that abstract members represent behavior (methods) to be learned and knowledge or beliefs (properties) to be acquired. To Speak() is a learned behavior of a Human class object. A ChineseHuman may learn to Speak() differently than an EnglishHuman and neither knows how to Speak() just because they are both Human.

    Nuances:

    • virtual methods do NOT need to be overridden.
    • There is no such thing as a virtual class.
    • abstract members can only appear on abstract classes. In the above examples, having an abstract method on the Human class implies that Human is an abstract class, and that, therefore, a Human cannot be instantiated using the phrase var baby = new Human();. Instead, the BabyHuman class should inherit from Human, and it should be instantiated as var baby = new BabyHuman();. Because a BabyHuman() is a Human and EnglishHuman and ChineseHuman both also inherit from Human, EnglishHuman could inherit from BabyHuman instead of Human. Being Human is abstract because we are all something more than simply Human.
    • abstract members cannot be hidden, only their override implementations may be (further up the inheritance chain). For example, BabyHuman must implement the abstract Speak() method as an override. If EnglishHuman inherits from BabyHuman, it may then hide the BabyHuman implementation of Speak() with its own implementation by using the new keyword (see "Method Hiding in C#" reference below).
    • abstract classes can have virtual members. That's a main distinction between an interface and an abstract class. In that sense, an abstract class can define both a contract and a template of the class' behavior, whereas an interface only defines a contract.

    Code Reference:

0 comments:

Post a Comment