If one flags a component or method as final, then the intent is that that component or method cannot be extended by a sub component (or a method in same). This is a common feature in OO languages, although was never initially implemented in CFML's OO implementation. I really question the merits of adding it to CFML now, really. Beyond the idea that "well it should have been there", it's kind of only relevant for people building APIs, which doesn't often happen in CFML, and I've always found usage of final to be a pain in the arse more than anything actually useful. My position is I should be able to extend any class I like: it's not for the author of a class to pre-decide how I might end up using it.

Anyway, Lucee has added this in. I guess it makes sense if they've added in abstract classes and methods (another questionable, but slightly more useful, addition IMO).

Here the FinalComponent is declared as final, so it cannot be extended. So the code that just uses it unto itself works fine, but as soon as we try to use a CFC which extends it: error. And here's the output:

FinalComponent version
applicationyou cannot extend the final component [C:\scratch\blogExamples\lucee\5\cfcs\final\FinalComponent.cfc][]

Not the most polished of exception message there, but at least it works.

OK, so how about a final method? This is a method in a component which itself is not final, but the method is final:

As one might expect: trying to use code that extends that final method results in an error.

FinalMethod version
applicationthe function [F] from component [C:\scratch\blogExamples\lucee\5\cfcs\final\ExtendsFinalMethod.cfc] tries to overwrite a final method with the same name from component [C:\scratch\blogExamples\lucee\5\cfcs\final\FinalMethod.cfc][]

So far so good. Except the term is "override", chaps, not "overwrite".

Search This Blog

About

I've been a web developer since 2001. I have spent 13yrs as a CFML developer, and until Sept 2014, that was the main subject matter here. I've now been re-tasked as a PHP developer, so learning PHP will become the focus of this blog. But I also mess around with other languages too.

I tend to be a bit "forthright" in my opinions, I am indelicate, and I tend to swear too much. This will come out occasionally here: I make no apology for it.

Everything said here is my own opinion. Feel free to disagree with me :-)