Virtual vs Abstract
Virtual and Abstract are two keywords used in most Object Oriented (OO) programming languages such as Java and C#. Although there are slight differences in what it means in different languages, both Virtual and Abstract keywords provide a sense of partial implementation to the entities it attaches to.
Typically, Abstract classes, also known as Abstract Base Classes (ABC), cannot be instantiated (an instance of that class cannot be created). So, Abstract classes are only meaningful to have if the programming language supports inheritance (ability to create subclasses from extending a class). Abstract classes usually represent an abstract concept or entity with partial or no implementation. Therefore, Abstract classes act as parent classes from which child classes are derived, so that the child class will share the incomplete features of the parent class and functionality can be added to complete them. Abstract classes may contain Abstract methods. Subclasses extending an abstract class may implement these (inherited) Abstract methods. If the child class implements all such Abstract methods, it is a concrete class. But if it does not, the child class also becomes an Abstract class. What all this means is that, when the programmer nominates a class as an Abstract, she is saying that the class will be incomplete and it will have elements that need to be completed by the inheriting subclasses. This is a nice way to create a contract between two programmers, which simplifies tasks in software development. The programmer, who writes code to inherit, needs to follow the method definitions exactly (but of course can have her own implementation). In Java and C#, Abstract classes and methods are declared using Abstract keyword.
Virtual methods/functions provide the ability to optionally override its behavior by an inheriting class (using a function with the same signature). The concept of Virtual function is important in the following scenario. Suppose a class is derived by a child class, then whenever an object of the derived class is used, it may refer to an object of the base class or the derived class. But, method call behavior can be ambiguous if the base class methods are overridden. So, in order to resolve this ambiguity, Virtual keyword is used. If the method is marked Virtual, then the derived class’s function is called (if any) or else the base class’s function is called. For example, in C++, Virtual keyword is used exactly for this purpose. In C#, the Virtual keyword is used in a similar way, but in addition, the keyword override should be used to modify all overridden methods. But in Java, there is no explicit Virtual keyword. All non-static methods are considered Virtual. Virtual functions with no body are called Pure Virtual functions. In Java and C#, Abstract methods are in fact Pure Virtual.
Difference between Virtual and Abstract
Although Abstract and Virtual are two keywords/concepts that provide a meaning of incomplete implementation to its associated entities, they do have their differences. Abstract methods (that must be defined inside Abstract classes) do not have an implementation at all, while Virtual methods may have an implementation. If Abstract methods are extended by a concrete class, all inherited Abstract methods must be implemented, while inherited Virtual methods may or may not be overridden.