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. |