Final Constructs
Both Adobe 2018 and Lucee Engines support the usage of
final
constructs for three contexts:- Components
- Methods
- Variables (Constants)
Final modifiers disallow the modifications to the source code to maintain stricter programming constructs. This would be used when you do not want to allow code to override your component or function or variable. This can be a great asset if you are building libraries, frameworks or APIs that require fine granular control of how they can be extended or used.
Components can be declared as final, meaning they cannot be extended (inheritance) by other components. This is the ultimate code reuse blocker! However, a final component CAN extend other components.
Final components can be used to prevent inheritance where it is not allowed. Great for APIs, frameworks, and libraries where the author wants to be strict about the usage of such code templates.
final component{}
final component extends="MyService"{}
Unlike
abstract
a function can be final
even if the component is not final
.Functions within a component can also be declared as
final
. Final methods cannot be overridden by sub-components. Final methods can be used to limit the extent to which sub-components redefine the behavior of the parent classes.component BaseUtil{
final function getFile(){
return getCurrentTemplatePath();
}