Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final!

 
kalyan v
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
To make a java class non subclassable (final class), we can use final. But any other ways to make a class final.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe making the class private too will work..
why do you need another way?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only inner classes can be made private. Marking the class as final should suffice. The methods within the class will also be implicitly final.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"kaylan v"
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy, particularly this section: "For your publicly displayed name, use a first name, a space, and a last name."
You can change it here.
Thanks! and again welcome to the JavaRanch!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kalyan,
If you were to make the constructor(s) unavailable, the class would then be un-subclassable.
Instances of the class could still be obtained through some static method.
A class based on the singleton pattern, might choose to make it's contructor(s) private, and then control the any instance creation of the class through a static method (often named getInstance).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic