1. Introduction

With the emergence of the object-oriented programming paradigm, multiple features regarding classes, attributes, and methods were made available to programmers. 

In this tutorial, we’ll investigate both abstract and virtual methods. First, we’ll briefly review basic concepts regarding the object-oriented paradigm. Next, we’ll study what particularly are abstract methods and virtual methods. Finally, we’ll systematically compare these concepts.

2. A Brief Review of Background Concepts

Understanding a computer program through a collection of objects that relates to each other brings several opportunities. However, this paradigm also brings lots of new challenges.

One relevant challenge consists of absorbing the multiple and even similar concepts proposed for the object-programming paradigm. For example, the similarities, differences, and the relationship between abstract and virtual methods are often considered confusing concepts.

Let’s remember some elementary concepts about the object-oriented paradigm before going deeper into abstract and virtual methods. So, the most relevant concepts that we must have in mind are listed in the following subsections.

2.1. Class

We can see a class as a blueprint of data and functions. Classes may represent an object of the real world, such as a car. However, they can also express something that does not have a concrete representation in the real world, such as a network socket.

2.2. Attribute

We can understand attributes as the set of data representing the states or qualities of a class. In this way, attributes belong to classes.

2.3. Method

In the context of object-oriented programming, methods are procedures of specific classes. Simply, they are the actions executable through a class.

2.4. Object

Finally, objects are instances of classes. Objects make concrete the planning done in a class. Through them, we can typically manipulate attributes and execute methods.

3. Abstract Methods and Virtual Methods

First of all, both abstract and virtual methods are variations of traditional methods in the context of the object-programming paradigm. In this way, they also represent actions planned for a class.

But, abstract and virtual methods have different characteristics, most in terms of implementation, compared to traditional methods. Let’s explore them in this section.

3.1. Abstract Methods

In summary, abstract methods do not require implementation. So, we can only declare them in a class, only presenting their signature: name and arguments.

Abstract methods are declared in the context of abstract classes. Abstract classes are then inherited by other classes. So, these other classes can use both the attributes and methods defined in the abstract class.

However, if the inherited abstract class contains one or more abstract methods, the programmers using it must implement these methods in the context of the descending class.

Thus, we can see abstract methods as templates indicating that determined functionalities will exist with predefined arguments. However, they will be implemented considering the distinct necessities of the class inheriting them.

The following image illustrates a simple example of how abstract methods work:


3.2. Virtual Methods

Virtual methods have their implementations provided together with their signatures in a class, different from the abstract ones.

It means that virtual methods provide a native set of operations executed when called. So, they do not require new implementation in the context of the descendants’ classes in inheritance relations.

However, although not requiring a mandatory implementation, classes inheriting other classes can override the virtual methods from the latter. So, descending classes keep the same virtual methods signature from the ascendents’ ones but can replace their operations.

Thus, besides defining functionalities, like abstract methods, virtual methods also provide standard operations implementing these functionalities. These operations, in turn, are replaceable according to the necessities of the class inheriting them.

The following image depicts an example of virtual methods:


3.3. Systematic Summary

In short, we can see abstract methods as blueprints providing method signatures in abstract classes. Programmers developing classes that inherit abstract classes with one or more abstract methods, in turn, must implement the abstract method in the descending classes.

Virtual methods are methods provided with signature and implementation. So, a class inheriting another class with a virtual method can execute the latter with no implementation effort. But, the programmer can override the implementation of virtual methods in the descending class if necessary.

The following table summarizes the main differences between abstract and virtual methods:

Rendered by QuickLaTeX.com

4. Conclusion

In this tutorial, we learned about abstract and virtual methods. At first, we studied some background concepts regarding the object-oriented programming paradigm. Thus, we investigated the definition of abstract and virtual methods. Finally, we compared both categories of methods in a systematic summary.

So, we can conclude that abstract and virtual methods are valuable resources of the object-oriented programming paradigm. Both abstract and virtual methods are strongly related to the concepts of inheritance and polymorphism in this paradigm.

Furthermore, these method categories guide the programmers in implementing descending classes, providing blueprints and sketches to them.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.