joydip_kanjilal
Contributor

Exploring virtual and abstract methods in C#

opinion
Mar 11, 20155 mins
Software Development

Take advantage of abstract methods in C# to force the subclasses to have implementations

The C# programming language provides support for both virtual and abstract methods, each of which has distinct advantages. You use virtual methods to implement late binding, whereas abstract methods enable you to force the subclasses of the type to have the method explicitly overridden. In this post, I will present a discussion on both virtual and abstract methods and when they should be used.

A virtual method is one that is declared as virtual in the base class. A method is declared as virtual by specifying the keyword “virtual” in the method signature. A virtual method may or may not have a return type. Virtual methods allow subclasses of the type to override the method. They are used to implement run time polymorphism or late binding. It should be noted that virtual or abstract members of a class cannot be declared as private. Also, you can have an implementation in a virtual method, i.e., virtual methods can have implementations in them. These implementations can be overridden by the subclasses of the type in which the virtual method has been defined.

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

Let’s now dig into some code for a better clarity on how virtual methods are used. Refer to the code snippet below.

public class Base { public virtual void Test() { Console.WriteLine("This is the base version of the virtual method"); } } public class Derived : Base { public override void Test() { Console.WriteLine("This is the derived version of the virtual method"); } } The Test() method is declared as virtual in the Base class and is overridden in the Derived class. Note how the virtual keyword is used to declare the method as virtual in the Base class. The virtual keyword is not needed when you override the virtual method in the Derived class.

Now, refer to the code snippet given next that illustrates how virtual methods are called. class Program { static void Main() { Base baseObj1 = new Base(); baseObj1.Test(); Base baseObj2 = new Derived(); baseObj2.Test(); } }

Note that two instances of the Base class are created — baseObj1 and baseObj2. In the first case, the reference object named baseObj1 refers to an instance of the Base class. In the second case, the reference object named baseObj2 refers to an instance of the Derived class. When you execute the code, the first call to the virtual method would display the message “This is the base version of the virtual method” in the console. In the second case, the message “This is the derived version of the virtual method” would be displayed. Why this difference?

In the first case, the type of the reference object baseObj1 is considered — as it is of Base type, the base version of the virtual method will be called. In the second case, the context of the reference object baseObj2 will be considered and hence the result.

Abstract methods are those that are declared abstract in the base class and cannot have implementations in them, i.e., they cannot have any functionality in them. You can use abstract methods when you would like the method to be overridden forcefully in the derived classes of the type in which the abstract method has been defined. This is enforced at compile time by the compiler. So, if you have declared a method as abstract using the abstract modifier in a base class, the subclasses of this class would have to implement the abstract method failing which the compiler would display an error stating that the derived class hasn’t implemented the abstract member. In essence, an abstract method is declared using the abstract keyword in an abstract base class and non-abstract subclasses of this type have to have their own implementation of the abstract method. Abstract methods are also implicitly virtual in nature but you cannot use the virtual keyword when declaring an abstract method. It should be noted that abstract methods can only be declared inside abstract classes.

A typical use of an abstract method is to force overriding of the ToString() or Equals() methods. The following code snippet illustrates how abstract methods are declared in an abstract class named EntityBase.

public abstract class EntityBase { public abstract override string ToString(); public abstract override bool Equals(object obj); } public class Customer : EntityBase { //Implementation code for the abstract methods } The EntityBase class is the base type for all entities — the Customer entity class extends this class and provides implementation for the abstract methods. In essence, all entity classes would provide their own implementation of the ToString() and Equals() methods. No default implementation for these methods are needed in the base class and hence they are marked as abstract. So, method overriding is enforced by declaring the method as abstract in the base class named EntityBase.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author