GridData class in SWT
This is about GridData class in SWT UI programming. Why 开发者_开发技巧should GridData be final ?
Java Doc of GridData says that it's final. I want to understand why it should be final.
Thank and Regards
Frankly there is no reason not to be final, It's common behavior of API programmers to make all classes which are not determined to be extended.
Read When should one use final? and Using “final” modifier whenever applicable in java stackoverflow questions for more info. It also relates with SWT subclassing FAQ.
Sorceror is correct. From an API point-of-view, you always want to make everything final to prevent future extensions of the API because somebody have been creative in their sub-classing...
In this particular example the reason is likely to be that the designer though he might need additional members in the class in some future. E.g. there are minimumHeight
and minimumWidth
in the current version, but no maximumHeight
and maximumWidth
which could make sense as well... But what if you or somebody else had extended GridData
with private members with these exact names? Then the API extension would not be forward compatible and a lot of code could potentially break this way.
精彩评论