aspose file tools*
The moose likes Beginning Java and the fly likes Why not using private access specifier instead of final!! I have a doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why not using private access specifier instead of final!! I have a doubt" Watch "Why not using private access specifier instead of final!! I have a doubt" New topic
Author

Why not using private access specifier instead of final!! I have a doubt

Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
Hello Ranchers, I have come to know that If I do not want my class or to be subclassed or further extended ,or I feel its perfect and no one should modify it, I should use the key word "final".why cant't i just declare my class or method as private. wouldn't that be the same. If not why? and what is the difference between final and private access specifier.
Looking forward for you kind help.


****************************<br />For Some Dreams.....<br />One Lifetime Is Not Enough.<br />****************************
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14113
    
  16

Note that you cannot make top-level classes private (only nested classes can be made private). Making a nested class private does not prevent it from being subclassed. For example:

Also, making methods private does not help to prevent a class from being subclassed.

There are two ways to make a class impossible to extend: (1) make it final, (2) make all constructors private.

The first way (making it final) is preferred, because this is exactly what the "final" keyword used on a class is meant for. With the second way, you're not directly specifying that you want a class to be impossible to subclass - it is merely a secondary effect, because subclasses can't access any constructor of your class.

If you use the first way, you'll get a much clearer error message from the compiler if you make a mistake and try to subclass the class.
[ January 10, 2008: Message edited by: Jesper Young ]

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

Joined: Dec 29, 2007
Posts: 60
Jesper Young , thank you for your kind information. Now i get a much clearer picture.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
By declaring a method private, you are saying that the method is for internal use only. This has a side effect of making it impossible to override the method as a subclass would have no visibility of the method.

You could make the class effectively final by declaring all its methods private. Yes, it could be extended, but none of its methods would be visible let alone overridden. However, it's best to declare the class final and use the private keyword for its proper purpose.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why not using private access specifier instead of final!! I have a doubt