tags:

views:

2193

answers:

9

Why wouldn't I choose abstract? What are the limitations to declaring a class member virtual? Can only methods be declared virtual?

A: 

If you want to give it an implementation in your base class you make it virtual, if you don't you make it abstract.

Yes, only methods can be declared virtual.

Maximilian
+1  A: 

You would use abstract if you do not want to define any implementation in the base class and want to force it to be defined in any derived classes. Define it as a virtual if you want to provide a default implementatio that can be overriden by derived classes.

Yes, only methods can be virtual.

Phil Wright
A: 

Abstract means that you can't provide a default implementation. This in turn means that all subclasses must provide an implementation of the abstract method in order to be instantiable (concrete).

I'm not sure what you mean by 'limitations', so can't answer that point.

Properties can be declared virtual, but you can conceptually think of them as methods too.

Seb Rose
+2  A: 

A member should be declared virtual if there is a base implementation, but there is a possibility of that functionality being overridden in a child class. Virtual can also be used instead of abstract to allow a method implementation to be optional (ie. the base implementation is an empty method)

There is no limitation when setting a member as virtual, but virtual members are slower than non-virtual methods.

Both methods and properties can be marked as virtual.

Richard Szalay
A: 

First of all, I will answer you second question. Only methods can be declared virtual. You would choose virtual instead of abstract when you want some default functionality in your base class, but you want to leave the option of overriding this functionality by classes that inherit from your base class. For examples:

If you are implementing the Shape class, you would probably have a method called getArea() that returns the area of your shape. In this case, there's no default behavior for the getArea() method in the Shape class, so you would implement it as abstract. Implementing a method as abstract will prevent you to instantiate a Shape object.

On the other hand, if you implement the class Dog, you may want to implement the method Bark() in this case, you may want to implement a default barking sound and put it in the Dog class, while some inherited classes, like the class Chiwawa may want to override this method and implement a specific barking sound. In this case, the method bark will be implemented as virtual and you will be able to instantiate Dogs as well as Chiwawas.

Sakin
+3  A: 

An abstract method or property (both can be virtual or abstract) can only be declared in an abstract class and cannot have a body, i.e. you can't implement it in your abstract class.

A virtual method or property must have a body, i.e. you must provide an implementation (even if the body is empty).

If someone want to use your abstract class, he will have to implement a class that inherits from it and explicitly implement the abstract methods and properties but can chose to not override the virtual methods and properties.

Exemple :

using System;
using C=System.Console;

namespace Foo
{
    public class Bar
    {
     public static void Main(string[] args)
     {
      myImplementationOfTest miot = new myImplementationOfTest();
      miot.myVirtualMethod();
      miot.myOtherVirtualMethod();
      miot.myProperty = 42;
      miot.myAbstractMethod();
     }
    }

    public abstract class test
    {
     public abstract int myProperty
     {
      get;
      set;
     }

     public abstract void myAbstractMethod();

     public virtual void myVirtualMethod()
     {
      C.WriteLine("foo");
     }

     public virtual void myOtherVirtualMethod()
     {
     }
    }

    public class myImplementationOfTest : test
    {
     private int _foo;
     public override int myProperty
     {
      get { return _foo; }
      set { _foo = value; }
     }

     public override void myAbstractMethod()
     {
      C.WriteLine(myProperty);
     }

     public override void myOtherVirtualMethod()
     {
      C.WriteLine("bar");
     }
    }
}
Kokuma
+1  A: 

There is a gotcha here to be aware of with Windows Forms.

If you want a Control/UserControl from which you can inherit, even if you have no logic in the base class, you don't want it abstract, because otherwise you won't be able to use the Designer in the derived classes: http://www.urbanpotato.net/default.aspx/document/2001

Benjol
A: 

You question is more related to style than technicalities. I think that this book http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756 has great discussion around your question and lots of others.

Hallgrim
A: 

I personally mark most methods and properties virtual. I use proxies and lazy loading alot, so I don't want to have to worry about changing things at a later date.

Chris Canal