This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes when to use final keyword & why ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "when to use final keyword & why ?" Watch "when to use final keyword & why ?" New topic
Author

when to use final keyword & why ?

Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
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


Thanks,
Prasath

SCJP1.4, SCWCD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Sometimes classes or methods are just not designed to be subclassed, or it makes no sense to do so. By making those classes final, the developer of an API can indicate to the users of the API that those classes aren't meant to be subclassed.

There are lots of examples in the standard API: for example String, StringBuffer, System are all final classes.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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/


Tony Morris
Java Q&A (FAQ, Trivia)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are some definite risks to extending concrete classes, especially if you don't own the superclass. If you extended ArrayList Sun could change ArrayList in the future in ways that maintain their own interface contracts but break your code. If you owned both classes and one broke the other you'd fix them. It's all just time and money. Plenty of people live with these risks and don't burst into flames, but it is surely safer to never extend a concrete class. If you buy into that rule you might as well enforce the rule by making your concrete classes final.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use final keyword & why ?
 
Similar Threads
overriding main
how to prevent overriding method?
About Final keyword
Final Keyword
Static methods and instance variables are not overridden