• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Difference between Private and Final

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am confused abt the use of Final and private keyword in java.
Are both use to prevent overridding??
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Example



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.


Yours,
Bu.



ps: upper class methods can also be wearing large hats at a horse race or polo match.
 
Naty wills
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for prompt answers..
Now all my doubts are cleared..
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This explanation helped a lot.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
difference between private and final method helped a lot. Thank you.
 
Marshal
Posts: 80254
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

It is nice to think that these old posts can still be useful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic