What is the difference between abstract and virtual method?

In the abstract method, we will implement things in the child class with the help of the override keyword. When it is implemented, the abstract and virtual methods don’t have any kind of difference. For example, if we inherit a class that is used to override the abstract class, we can override the method again. Similar to the way we override the virtual method.

The basic difference is they both are overridable methods, but the abstract method has no default implementation while the virtual method has the default implementation.

Virtual method

In the virtual method, we have the default implementation and we can override this with the help of the derived class. With the help of the override modifier, we can override this method.

Syntax

class Parent
{
virtual void test()
{
//Some Default Implementation
}
}
class Child : class Parent
{
void test()
{
//Override the Parent class test function
}
}

Code

#include <iostream>
using namespace std;
class A
{
public:
virtual void raw()
{
cout << "Base Function" << endl;
}
};
class B : public A
{
public:
void raw()
{
cout << "Derived Function" << endl;
}
};
int main(void)
{
B obj;
obj.raw();
return 0;
}

Explanation

  • Lines 3–10: We make a base class named class A, which has a virtual function, raw(), which is used to print the output.

  • Lines 12–19: We make a derived class named class B which is inherited from class A so that it uses the class A function to override it.

  • Lines 21–26: We make the object of class B and call the raw function of class B.

Abstract method

The abstract method doesn’t have the default implementation. Instead we have to override it with the help of the derived class. It doesn’t have the default code.

Syntax

class Parent
{
virtual void test() = 0; //Doesn't have any implementation
}
class Child : class Parent
{
void test()
{
//Override the Parent class test function
}
}

Code

#include <iostream>
using namespace std;
class A
{
public:
virtual void raw() = 0; //declaring a virtual function
};
class B : public A
{
public:
void raw()
{
cout << "Derived Function" << endl;
}
};
int main(void)
{
B obj;
obj.raw();
return 0;
}

Explanation

  • Lines 3–7: We make a base class named class A which has a virtual function, raw, which doesn’t have any default implementation.

  • Lines 9–16: We make a derived class, class B, which is inherited from class A, that uses the class A function to override it.

  • Lines 18–23: We make the object of class B and call the raw function of class B.

Following are the basic differences between abstract and virtual methods:

Abstract method


Abstract methods are declared but they don’t have an implementation.


It is compulsory to override the abstract method.


An abstract keyword is used to distinguish it from other methods.


The scope of the abstract method is for the members and classes.


We can only declare this method under the abstract class.


Virtual method


Virtual methods are used to implement type-based polymorphism.


It is not compulsory to override the abstract method.


Virtual keyword is used to distinguish it from other methods.


The scope of the virtual method is only limited to the members of the class.


We can declare this method under the abstract and non-abstract classes.


Free Resources