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. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos