Classes: Override / Final

code

Syntax

 

Usage / Usability

  • Override
    • Can be used only on virtual functions that are declared in a base class. This assures that the function is overriden and not overwritten by mistake.
    • Used to clearly express the intent that a given derived class function is meant to provide a new implementation for a base class function
    • Helps the compiler flag errors where one accidentally overloads rather than overrides
  • Final
    • In functions: Used so that the function cannot be overridden anymore
    • In classes: Used so that the class cannot be further derived from

PROS

  • Express a clear image on what the programmer wanted to do.

CONS

  • Adding final after a class or function is part of the function skeleton. In case we want to override it, we will break the compatibility with previous versions.

 

Quiz – Which lines will compile? (separately)

 

Answer: All except 4, 6, and 10.

Leave a comment

Your email address will not be published.