2

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

Vineel
  • 1,630
  • 5
  • 26
  • 47

2 Answers2

2

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.

Community
  • 1
  • 1
Sorceror
  • 4,835
  • 2
  • 21
  • 35
0

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.

Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70
  • Hi,thanks a lot for your explaination .. Could you suggest me what can I do , to become good at Object Oriented Design .. – Vineel Jul 08 '11 at 04:49
  • If you miss something in Layout manager, you use bad one. Try another layout managers, like MigLayout, I'm sure you will not miss there anything.. – Sorceror Jul 08 '11 at 07:47