views:

976

answers:

5

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

+2  A: 

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 { ... } }
JaredPar
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.
George2
Abstract members have no implementation, and so _must_ be overridden. Virtual members have an implementation, and _may_ be overridden. That's the difference.
Tor Haugen
Understand, MSDN words are so fuzzy. :-)
George2
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!
Coincoin
+1  A: 

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.

Fadeproof
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"?
George2
+8  A: 

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;
       }
}
Frederik Gheysels
So, you think MSDN means, for abstract base class, it is "must", but for non-abstract base class, it is "can" (not a must)?
George2
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.
Frederik Gheysels
+1  A: 

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.

Mike Hofer
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.
George2
It is not required to override virtual members.
Frederik Gheysels
@George: I clarified the answer to accomodate your question.@Frederik: Note that I said that "you can override" a virtual member.
Mike Hofer
@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
Frederik Gheysels
@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.
Mike Hofer
+1  A: 
EnocNRoll