Originally posted by prasath thirumoorthy:
Hi Friends,
I know that final keyword is used for
1.If it is used before variable act as constant.
2.If it is used before method Avoid overriding to its subclasses.
3.If it is used in the class declaration Avoid Inheritance.
But my question is why there is a need to avoid inheritance, avoid method overriding ? give me an real time example in which case/situtation it will be useful.
Thanks & Regards,
Prasath T
Final is not applied to variables to act as a constant; they are applied to 'variables' (to misuse terminology as the JLS has) to produce 'write once' fields, 'write once or not at all' locals, or constants. They are all very different things.
It is also not used to prevent inheritance, rather, to prevent concrete inheritance, which is different from legitimate inheritance. The argument regarding the illegitimacy of concrete inheritance is very controversial, mainly because of reasons that I am not permitted to say on this forum I assume due to the lack of orthodoxy and contradiction of critical mass belief, so instead I refer you to the search button above and google.
When a final is not a constant
http://jqa.tmorris.net/trivia/GetQAndA.actio?qids=13 Why extends is not evil
http://www.tmorris.net/pubs/extends-not-evil/