Why is StringBuilder final versus all final methods?

The motivation for this was my answer ("Desired thinking") to an earlier question about StringBuilder's best methods . If StringBuilder was extensible, then domain-specific subclasses could extend their free interface to tighten the code where StringBuilder is passed to many methods that create parts of a larger string.

I am considering offering something - perhaps a StringBuilder delegate - for the Guava people.

What additional purpose does StringBuilder have to be final, and not just with final methods?

+7
source share
1 answer

If all methods are final, the current behavior of the StringBuilder cannot be changed. Subclasses of it should add either unrelated behavior (in fact, poor design), or new functionality that uses old, for example, convenience methods. If you want to do the latter, it's probably best to have a class that provides the appropriate functionality, but contains a StringBuilder rather than extending it. As Joshua Bloch says, "they prefer containment over inheritance." In short, if all methods are final, there is no good reason to extend the class, and you can also make it final.

+9
source

All Articles