There is a nice article about "When to declare classes final". A few quotes from it:
TL;DR: Make your classes always
final, if they implement an interface, and no other public methods are definedWhy do I have to use
final?
- Preventing massive inheritance chain of doom
- Encouraging composition
- Force the developer to think about user public API
- Force the developer to shrink an object's public API
- A
finalclass can always be made extensibleextendsbreaks encapsulation- You don't need that flexibility
- You are free to change the code
When to avoid
final:Final classes only work effectively under following assumptions:
- There is an abstraction (interface) that the final class implements
- All of the public API of the final class is part of that interface
If one of these two pre-conditions is missing, then you will likely reach a point in time when you will make the class extensible, as your code is not truly relying on abstractions.
P.S. Thanks to @ocramius for great reading!
Answer from Nikita U. on Stack OverflowThere is a nice article about "When to declare classes final". A few quotes from it:
TL;DR: Make your classes always
final, if they implement an interface, and no other public methods are definedWhy do I have to use
final?
- Preventing massive inheritance chain of doom
- Encouraging composition
- Force the developer to think about user public API
- Force the developer to shrink an object's public API
- A
finalclass can always be made extensibleextendsbreaks encapsulation- You don't need that flexibility
- You are free to change the code
When to avoid
final:Final classes only work effectively under following assumptions:
- There is an abstraction (interface) that the final class implements
- All of the public API of the final class is part of that interface
If one of these two pre-conditions is missing, then you will likely reach a point in time when you will make the class extensible, as your code is not truly relying on abstractions.
P.S. Thanks to @ocramius for great reading!
For general usage, I would recommend against making a class final. There might be some use cases where it makes sense: if you design a complex API / framework and want to make sure that users of your framework can override only the parts of the functionality that you want them to control it might make sense for you to restrict this possibility and make certain base classes final.
e.g. if you have an Integer class, it might make sense to make that final in order to keep users of your framework form overriding, say, the add(...) method in your class.
Videos
I have often read that classes should only be made 'final' on rare/special occasions.
Whoever wrote that is wrong. Use final liberally, there’s nothing wrong with that. It documents that a class wasn’t designed with inheritance in mind, and this is usually true for all classes by default: designing a class that can be meaningfully inherited from takes more than just removing a final specifier; it takes a lot of care.
So using final by default is by no means bad. In fact, amongst OOP experts it’s widely agreed that final should be the default, e.g. Jon Skeet:
Classes should be sealed by default in C#
Or Joshua Bloch:
Design and document for inheritance or else prohibit it [Effective Java, 3rd Ed, Item 19]
Or Scott Meyers [More Effective C++, Item 33].
Which is why modern OO langauges such as Kotlin have final-by-default classes.
You wrote:
Maybe it screws up Mock object creation …
And this is indeed a caveat, but you can always recourse to interfaces if you need to mock your classes. This is certainly superior to making all classes open to inheritance just for the purpose of mocking.
If you want to leave a note to yourself that a class has no sub-classes, then by all means do so and use a comment, thats what they are for. The "final" keyword is not a comment, and using language keywords just to signal something to you (and only you would ever know what it means) is a bad idea.
edited by original author to add: I completely disagree with this now. I cannot even create a model of my mental state 11 years ago that would explain why I would say this. I think this answer, and my comments defending it below, are ridiculous. The accepted answer is right.