What is the difference between the Virtual method and the Abstract method?
676
16-Nov-2020
Aryan Kumar
06-Jun-2023Virtual and abstract methods are both concepts in object-oriented programming (OOP) that allow for code to be reused and for polymorphism to be implemented. However, there are some key differences between the two.
A virtual method is a method that can be overridden by a subclass. This means that the subclass can provide its own implementation of the method. A virtual method is declared using the virtual keyword. For example, the following code declares a virtual method called Bar() in the Foo class:
Code snippet
A subclass can override the Bar() method by simply declaring a method with the same name and signature. For example, the following code declares a subclass of Foo called Bar that overrides the Bar() method:
Code snippet
When an object of type Bar is created and the Bar() method is called, the implementation of Bar() in the Bar class will be called.
An abstract method is a method that cannot be overridden by a subclass. This means that the subclass must provide its own implementation of the method, or it will not compile. An abstract method is declared using the abstract keyword. For example, the following code declares an abstract method called Baz() in the Foo class:
Code snippet
A subclass of Foo cannot simply declare a method with the same name and signature as Baz(). The subclass must also provide its own implementation of Baz(). For example, the following code declares a subclass of Foo called Baz that provides an implementation of the Baz() method:
Code snippet
When an object of type Baz is created and the Baz() method is called, the implementation of Baz() in the Baz class will be called.
The main difference between virtual and abstract methods is that virtual methods can be overridden by subclasses, while abstract methods cannot. This means that virtual methods provide more flexibility, as subclasses can choose to implement the method in their own way. Abstract methods, on the other hand, force subclasses to provide an implementation of the method, which can make code more rigid.
Here is a table that summarizes the key differences between virtual and abstract methods:
Ultimately, the best type of method to use will depend on the specific requirements of the situation. If the implementation of a method can vary between subclasses, then a virtual method should be used. If the implementation of a method must be provided by a subclass, then an abstract method should be used.
Rahul Roi
16-Nov-2020Where the Virtual method must always have a default implementation. Although, this can be overridden in the derived class, however, it is not mandatory. This can be overridden using the override keyword.
Where an Abstract method does not have any implementation. This resides in the abstract class. This is mandatory that the derived class implements the abstract method. The override keyword is not necessary here though it can be used.