Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn The Final Keyword | Inheritance Overview
C++ OOP
course content

Course Content

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
The Final Keyword

The final keyword plays a crucial role in defining classes and controlling inheritance hierarchies. It provides developers with a powerful mechanism to restrict certain aspects of class design.

The usage of final keyword

When a class is declared as final, it indicates that the class cannot be subclassed further. In other words, it serves as the end point of the inheritance hierarchy, disallowing any further derivation.

Example.cpp

Example.cpp

copy
12345678
class Example final { // Class members and methods }; // cannot derive from final base Example class ExtendedExample: public Example { // Class members and methods };

This feature is especially valuable when you aim to restrict the modification or extension of a specific class, or when you prefer using your class through composition rather than inheritance. It also offers other benefits, such as improving code clarity and enabling certain compiler optimizations.

question mark

What does the final keyword do when applied to a class?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

course content

Course Content

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
The Final Keyword

The final keyword plays a crucial role in defining classes and controlling inheritance hierarchies. It provides developers with a powerful mechanism to restrict certain aspects of class design.

The usage of final keyword

When a class is declared as final, it indicates that the class cannot be subclassed further. In other words, it serves as the end point of the inheritance hierarchy, disallowing any further derivation.

Example.cpp

Example.cpp

copy
12345678
class Example final { // Class members and methods }; // cannot derive from final base Example class ExtendedExample: public Example { // Class members and methods };

This feature is especially valuable when you aim to restrict the modification or extension of a specific class, or when you prefer using your class through composition rather than inheritance. It also offers other benefits, such as improving code clarity and enabling certain compiler optimizations.

question mark

What does the final keyword do when applied to a class?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6
some-alt