What is the difference between virtual and abstract keywords in .NET?

The two keywords virtual and abstract are used in inheritance. An interesting feature about inheritance is that derived class can override the existing behavior of base class.




For a method to be overridden in base class, it should be defined as virtual or abstract in the base class. But what is the difference between these two keywords? This is tabulated below:

Virtual
Abstract
If you feel that the derived class may or may not override the base class method, then you will define the base class method as virtual. Consider the following example:
namespace Application1 {
public class virtualClass {
public virtual void virtualMethod(){
Console.WriteLine("Virtual Method..");
}
}
public class derivedClass:virtualClass{
public override void virtualMethod(){
Console.WriteLine("Overridden..");
}
}
public class testClass {
public static void Main() {
virtualClass obj = new virtualClass();
obj.virtualMethod();
derivedClass dObj = new
derivedClass();
dObj.virtualMethod();
}
}
}
Output of this code will be:
Virtual Method..
Overridden..
But if you want to enforce that derived class must override the base class method then you will define the base class method as abstract.
namespace Application1 {
public abstract class abstractClass {
public abstract void abstractMethod();
}
public class derivedClass:abstractClass{
public override void abstractMethod(){
Console.WriteLine("Overridden..");
}
}
public class testClass {
public static void Main() {
derivedClass obj = new
derivedClass();
obj.abstractMethod();
}
}
}
Output of this code will be:
Overridden..
Virtual methods need not be compulsorily overridden. In the above example if the derived class does not override the method virtualMethod, then again the code will work. Abstract methods should compulsorily be overridden by the derived class. In the above example, if the derivedClass does not override abstractMethod, then during compilation you will get the following error:
'Application1.derivedClass' does not implement inherited abstract member 'Application1.abstractClass.abstractMethod()'
To define a base class method to be virtual, you need not include any new definition for the base class. In the earlier example, you can see that the class virtualClass just includes an access modifier followed by the class name. No other additional modifiers are required. If you want to define a method as abstract in the base class then the base class should also be marked as abstract. Consider the following example:
namespace Application1 {
public class abstractClass {
public abstract void abstractMethod();
}
}
In this example, abstractClass has an abstract method. Hence the class in itself has to be marked abstract. But it is not done in the above example. Therefore during compilation, you will end up in the following error:
'Application1.abstractClass.abstractMethod()' is abstract but it is contained in nonabstract class 'Application1.abstractClass'
Virtual method can have a method body. In the earlier example, virtualMethod of virtualClass has method definition like any of the other methods that you define and it is perfectly legal. Abstract methods have only the signature. It cannot have method body. However the abstract class can include non-abstract methods and these methods can have method body defined. Consider the following example:
namespace Application1 {
public abstract class abstractClass {
public abstract void abstractMethod(){
Console.WriteLine("Abstract
method..");
}
}
}
Here you are trying to include method body for an abstract method. This is not permissible. Hence during compilation you will end up in the following error:
"'Application1.abstractClass.abstractMethod()' cannot declare a body because it is marked abstract"
Class containing virtual method can be instantiated. Here is an example:
namespace Application1 {
public class virtualClass {
public virtual void virtualMethod(){
Console.WriteLine("Virtual Method..");
}
}
Public class testClass {
public static void Main() {
virtualClass obj = new virtualClass();
obj.virtualMethod();
}
}
}
Output of this code will be:
Virtual Method…


Class containing abstract method cannot be instantiated. It can only be inherited. Consider the following example:
namespace Application1 {
public abstract class abstractClass {
public abstract void abstractMethod();
}
public class testClass {
public static void Main() {
abstractClass obj = new abstractClass();
}
}
}
Here you are trying to create an instance of abstract class. This is not possible. Hence during compilation you will end up in the following error:
"cannot create an instance of the abstract class or interface Application1.abstractClass"
Not just methods, virtual modifier can also be used with properties. Apart from class and method, the modifer abstract can be associated with properties, events and indexers.

There is a restriction when using virtual modifer. You cannot use this modifer along with static or abstract or override modifiers. Abstract modifiers also have a restriction. They cannot be used along with static or virtual or override modifiers.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| What is Private Access Modifier in C#? | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.