Zieglers

Just little about C#, .NET, SQL Server, SharePoint and SAP

Virtual vs. Abstract Classes

Posted by zieglers on March 25, 2010

Here is a brief comparison of VIRTUAL and ABSTRACT classes:

————–

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class.

The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Abstract classes have the following features:
– An abstract class cannot be instantiated.
– An abstract class may contain abstract methods and accessors.
– It is not possible to modify an abstract class with the sealed (C# Reference) modifier, which means that the class cannot be inherited.
– A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

In general, virtual does not work on classes.
It is only for a class method, property, indexer or event declaration to have the possibility, to override them in a derived class.
An abstract class can’t be initiated and can more interpretated as a template of a class, which has to be derived to create an instance of it. 
————–

Zieglers

Advertisements

One Response to “Virtual vs. Abstract Classes”

  1. […] Virtual vs. Abstract Classes […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: