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
-
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 headMike 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...
virtualvs.abstract:If a class method (or property) is marked
virtual, then it may be overridden using theoverridekeyword, if you choose to inherit (aka derive) from that class.
Thevirtualkeyword is intended to evoke the idea that the method may or may not be the actual method called. Therefore, I always think ofvirtualmembers as default implementations, meaning that it represents functionality that can be generalized, such as anEat()method on aHumanclass, which might involve eating with one's hands. However, aChineseHumanclass might override the default implementation ofEat(), 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. AllHumanobjects must know how toEat().
An object-oriented way of thinking might declare thatvirtualmembers represent instincts. ToEat()is an instinct of aHumanclass object. AChineseHumanmay learn toEat()with chop sticks.If a class method (or property) is marked
abstract, then it must be overridden using theoverridekeyword, if you choose to inherit from that class.
Theabstractkeyword 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,abstractmembers are only conceptual, and therefore they lack an implementation. It is a little confusing that C# asks us tooverrideabstract 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 anabstractmember of aHumanclass might beSpeak(). There would not be a common way of speaking for allHumanobjects, nor is it instinctual, because it requires language to express. Note: Some might argue thatSpeak()belongs on aninterfaceinstead.
An object-oriented way of thinking might declare thatabstractmembers represent behavior (methods) to be learned and knowledge or beliefs (properties) to be acquired. ToSpeak()is a learned behavior of aHumanclass object. AChineseHumanmay learn toSpeak()differently than anEnglishHumanand neither knows how toSpeak()just because they are bothHuman.
Nuances:
virtualmethods do NOT need to be overridden.- There is no such thing as a
virtualclass. abstractmembers can only appear onabstractclasses. In the above examples, having anabstractmethod on theHumanclass implies thatHumanis anabstractclass, and that, therefore, aHumancannot be instantiated using the phrasevar baby = new Human();. Instead, theBabyHumanclass should inherit fromHuman, and it should be instantiated asvar baby = new BabyHuman();. Because aBabyHuman()is aHumanandEnglishHumanandChineseHumanboth also inherit fromHuman,EnglishHumancould inherit fromBabyHumaninstead ofHuman. BeingHumanisabstractbecause we are all something more than simplyHuman.abstractmembers cannot be hidden, only theiroverrideimplementations may be (further up the inheritance chain). For example,BabyHumanmust implement theabstractSpeak()method as anoverride. IfEnglishHumaninherits fromBabyHuman, it may then hide theBabyHumanimplementation ofSpeak()with its own implementation by using thenewkeyword (see "Method Hiding in C#" reference below).abstractclasses can havevirtualmembers. That's a main distinction between aninterfaceand anabstractclass. In that sense, anabstractclass can define both a contract and a template of the class' behavior, whereas aninterfaceonly defines a contract.
Code Reference:
- Abstract Class versus Interface
By Rahman Mahmoodi
http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx - Method Hiding in C#
By Chetan Kudalkar
http://www.codeproject.com/KB/cs/cs_methodhiding.aspx
0 comments:
Post a Comment