Final Keyword in C++

Final Keyword in C++ with Examples:

In this article, I am going to discuss the Final Keyword in C++ with Examples. Please read our previous article where we discussed Auto Keyword in C++ with Examples. The Final Keyword is Introduced in C++ 11.

Final Keyword in C++

If you want to restrict your class to be inherited in the child class and if you want to restrict the Parent class method to be overridden in the child class, then you need to use the final keyword in C++. So, mainly the final keyword is used for two purposes. They are as follows:

  1. Restrict Class Inheritance
  2. Restrict Method Overriding
Restrict Class Inheritance in C++ using Final Keyword

Let us see how the final keyword restricts inheritance in C++ with an Example. Let us create a class called Parent as follows:
class Parent{
       void show (){
       }
};
And we have another class called Child which is inheriting from the Parent class as follows.
class Child : Parent{
        void show (){
        }
};
This concept is called Inheritance in C++ and we have already discussed inheritance in our previous articles. Now, let us see what happens if we write the final keyword after the Parent class as follows:
class Parent final{
          void show (){
         }
};
Once we make the Parent class final, then we cannot inherit any class from the Parent class as follows.
class Child : Parent{
          void show (){
          }
};
Now, inheriting the Parent class in the Child class will give an error that ‘Child’: cannot inherit from ‘Parent’ as it has been declared as ‘final’. So, one of the usages of the final keyword is it restricts inheritance. The complete Example code is given below.

#include <iostream>
using namespace std;
class Parent final
{
    void show ()
    {
    }
};

class Child:Parent
{
    void show ()
    {
    }
};

int main()
{
    return 0;
}
Output:

Restrict Class Inheritance in C++ using Final Keyword

Restrict Method Overriding using Final keyword in C++:

Now let us see another usage of the final keyword i.e. how the final keyword restricts the method overriding. We have a function called the show in the Parent class. This function is empty. We are using it as a dummy function. And inside the Child class also, we have defined the show function. So, this is known as function overriding. We already know about it. 

The Parent class is having function show and the Child class is also overriding the same Show function. If we write the final keyword after the show function in the Parent class as follows:
class Parent {
        void show () final {
        }
};

Now, when you marked the show function as virtual, you will get an error saying ‘Parent::show’: a ‘final’ function must be virtual. So, what it means is that a non-virtual function cannot be marked as final. Let us mark the function as virtual as follows:
class Parent {
        virtual void show () final {
        }
};

Now, let us try to override the show function in the child class as follows and see what happens.
class Child : Parent{
        void show (){
        }
};

Now we will get another error in the Child class saying ‘Parent::show’: function declared as ‘final’ cannot be overridden by ‘Child::show’. What it means is that you cannot override a final method in the child class. So, the final function of the Parent class cannot be overridden in the Child class. The complete example code is given below.

#include <iostream>
using namespace std;
class Parent {
    virtual void show () final {	
    }
};

class Child : Parent{
    void show (){	
    }
};

int main()
{
    return 0;
}
Output:

Final Keyword in C++ with Examples

So, the final keyword in C++ is used for two purposes. First, it is used in C++ for restricting class inheritance and 2nd it is used for restricting the final method of the Parent class to be overridden in the child class.

In the next article, I am going to discuss Lambda Expression in C++ with Examples. Here, in this article, I try to explain the Final Keyword in C++ with Examples and I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this Final Keyword in C++ with Examples article.

Leave a Reply

Your email address will not be published.