This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final for any class

 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the purpose of using final keyword before any class name, mqy be:
purpose 1. to make that class not extendiable further, or
purpose 2. to make that class immutable, by removing setter method from it.

My question is:
If I don't want to use 'final' keyword before any class name; can I achieve the above purposepurpose 1 by declaring its constructor as private ?
 
Campbell Ritchie
Sheriff
Posts: 48406
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't make a class immutable by putting final before the class name. Yes, you can prevent inheritance from a class by making all its constructors private.
But a user will see there is no "final" before the class name, and think it is possible to inherit from that class, so it is a bad idea to miss "final" out in that instance.
 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !!

~ramesh
 
Campbell Ritchie
Sheriff
Posts: 48406
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic