File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Difference between Private and Final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between Private and Final" Watch "Difference between Private and Final" New topic

Difference between Private and Final

Naty wills

Joined: Sep 20, 2005
Posts: 15
Hi all,
I am confused abt the use of Final and private keyword in java.
Are both use to prevent overridding??
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The purpose of "private" is to promote encapsulation. Member variables should almost always be private; class A should not be trying to touch the member data of class B.

"final" has a few different meanings. Applied to a variable, it prevents changes to the variable after initialization, making it a "constant." Applied to a method, it does, indeed, prevent overriding. Applied to a class, it prevents the class from being extended. All of these meanings have something in common: they allow the programmer to have fairly strong beliefs about the value or purpose of something. If you know a class can never be extended, then you know for sure how an instance of the class will behave -- you know you're not dealing with a subclass that changes something, since you know no subclasses can exist. This helps with both engineering and security.
[ September 05, 2006: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,
This is about final/private in methods only.

final does not allow overriding of methods. If you do, you'll get a compiler error.

private methods are not visible outside their classes. So when you extend a class, you can give a method the same declaration as in the upper class. It is a redefinition (no override!) with no relation to the upper class method.
Especially no polymorphism.

The bar method is only necessary to call foo from outside its class.

Outputs 3* ALPHA
Remove the private (-> default public or protected) and you'll have polymophic output.
make it final (in class Alpha), you'll get a compiler error in class Beta.


ps: upper class methods can also be wearing large hats at a horse race or polo match.

all events occur in real time
Naty wills

Joined: Sep 20, 2005
Posts: 15
Thanks a lot for prompt answers..
Now all my doubts are cleared..
supriya riya
Ranch Hand

Joined: Feb 23, 2009
Posts: 45
This explanation helped a lot.
S Nirmal

Joined: Nov 23, 2012
Posts: 1
difference between private and final method helped a lot. Thank you.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46320
Welcome to the Ranch

It is nice to think that these old posts can still be useful.
I agree. Here's the link:
subject: Difference between Private and Final
It's not a secret anymore!