C++20 sections · 1024 units
Open in Course

Final Keyword

Preventing further overrides

The final keyword prevents further overriding or inheritance. Mark a virtual function as final to stop derived classes from overriding it. Mark a class as final to prevent any class from inheriting from it.

Write void draw() override final; to allow overriding up to this point but no further. Derived classes attempting to override this function get a compile error. Use final when your design is complete and further extension would break assumptions.

It documents your intent and catches accidental overrides at compile time rather than runtime.