• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of Final Class  RSS feed

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi....
what is use of final class in java?and in Real-Time Scenario?
 
Amit ChaudhariC
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final class indicates that the class being marked as final cannot be subclassed. To put it simply you cannot create a subclass out of it.
For eg - final class A{}
class B extends A{} -- Compiler error
It deviates from the java principle of inheritance . You generally mark a class as final only if you want to make sure that nobody will be able to subclass it.
For eg - String is a final class meaning you cannot subclass it.
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And final classes are more kinda complete (atleast they assumed to be). String, all the primitive wrapper classes etc.. are good examples of them. And Welcome to Javaranch, Amit ChaudhariC
 
Sam Jimzz
Greenhorn
Posts: 28
Eclipse IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could any one tell me a real time example other than technical stuffs so that i can be more clear in how and where i can use FINAL..
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Sam

Did you mean real life, instead of real time?

you should *always* make your classes final, unless you intend your class to be extended, and you have properly designed it so that it is safe to extend it. This is actually not a simple task most of the time. When in doubt, make your class final.
 
Sam Jimzz
Greenhorn
Posts: 28
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThanQ

Ya i mean real life..sorry


you mean we will use final class's only when we don't need to extend it in the future..?
Is there any disadvantages in using more final class's in a project..?

Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Jimzz wrote:you mean we will use final class's only when we don't need to extend it in the future..?

This is one of the cases, sure. But making a class final doesn't mean you can't remove the final modifier again in the future. You can think of making a class final as saying "For now, I don't see any reason to extend this class".
However, once you make your class extensible, you can't go back to making it final. This is why you should always make a class final, unless you have a good reason not to.

Is there any disadvantages in using more final class's in a project..?

Yes, making them final means you can't extend them. However, this can be a hidden blessing, because inheritance breaks encapsulation, and encapsulation is much more important.
 
Sam Jimzz
Greenhorn
Posts: 28
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wowww... you are great..

This made me clear..

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!