The open annotation on a class is the opposite of Java’s final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to Effective Java, Item 17: Design and document for inheritance or else prohibit it.
Stephan van Hulst wrote:Making types extensible has to be a conscious choice. It's very difficult to get it right, and people actually tend to overuse inheritance when they should actually be using composition.
Ole Kristian Sandum wrote:Isn't this on the user of my classes, though? Opening for extension is not automatically contributing to misuse.
If a user of my class ends up in a bad situation because they could not be bothered to figure out the implications when they extended my class, then surely this programmer made a huge mistake?
As a library designer I always make sure to write thorough documentation.
Having to remember to declare my classes open all the time really would get tedious.
Valera Kostin wrote:Here is a big discussion thread about this question Classes final by default
And also the article from Andrey Breslau (Kotlin language designer) A bit about picking defaults
Stephan van Hulst wrote:Another reason why you should program against interfaces.
Ole Sandum wrote:And, to be honest, I don't think I should have to write an interface for the sole purpose of ONE class implementing it.
Stephan van Hulst wrote:Wouldn't you say that if you find it necessary to mock a class, it's complex enough that it warrants an interface anyway?
Stephan van Hulst wrote:When you're dealing with third party libraries, you don't have a choice anyway. Even so, if those classes are complex enough, it might be worth it to write a facade for them which *does* implement some interface.
Consider Paul's rocket mass heater. |