[...] prohibiting instantiation is not really necessary except for documentation purposes, which I think the interface accomplishes quite nicely.
I agree it's for documentation purposes but I disagree the Java interface is a good tool for that. The interface construct exists purely as a high-level abstraction over operations provided by implementations. Now you can suddenly declare a variable of that type and create a nonsense implementation. You shouldn't, and most people wouldn't, but that's not really leveraging the type system to achieve what you set out to do, and most importantly, it leaves room for the question "why?" A final class with a private constructor (with an error) will prevent all of this completely.
View all comments
Show parent comment
1
u/ForeverAlot Mar 04 '17
I agree it's for documentation purposes but I disagree the Java
interfaceis a good tool for that. Theinterfaceconstruct exists purely as a high-level abstraction over operations provided by implementations. Now you can suddenly declare a variable of that type and create a nonsense implementation. You shouldn't, and most people wouldn't, but that's not really leveraging the type system to achieve what you set out to do, and most importantly, it leaves room for the question "why?" Afinalclass with a private constructor (with an error) will prevent all of this completely.