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

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 from deriving it.

cpp

Example

copy
class Example final {
// Class members and methods
};

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

int main() {}
12345678910
class Example final { // Class members and methods }; // cannot derive from final base Example class ExtendedExample: public Example { // Class members and methods }; int main() {}

This feature is especially valuable when you aim to restrict the modification or extension of a specific class, or when you prefer utilizing your class using composition rather than inheritance. There is also other benefits to using it.

question mark

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

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 6

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

We use cookies to make your experience better!
some-alt