Vineel
Vineel

Reputation: 1788

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

Upvotes: 2

Views: 245

Answers (2)

Tonny Madsen
Tonny Madsen

Reputation: 12718

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.

Upvotes: 0

Sorceror
Sorceror

Reputation: 4843

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.

Upvotes: 2

Related Questions