• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Significance of 'final' modifier

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Could anybody clarify my following doubt?

Is there any significance of 'final' modifier for private instance methods?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mahendar Reddy:
Hi,Could anybody clarify my following doubt?

Is there any significance of 'final' modifier for private instance methods?



hi,

I think the use of final in the signature of private method is like to make method output constant.

For Example,

if one have private method which returns int value but creator of that method doesn't want to modify the value which is returning by the method for that one can put final modifier ahead of method signature.

Thanks
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would normally use the final modifier on a method to prevent anyone over-riding the method if they are extending your class.

Of course, a private method cannot be inherited so a final modifier is redundant in this case.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James_ Ryan:
I think the use of final in the signature of private method is like to make method output constant.


No, that is not at all what 'final' means when used on a method.

By making a method 'final', you can't override it in a subclass. But since private methods aren't visible to subclasses anyway, it makes no sense to make a private method 'final'.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually private methods are final by default.... This doesn't mean that you can't declare a method in a sub-class with the same signature as of the private method. You can do that as private methods are not inherited, so that wont be called as overriding...

 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats the way I see final:

a) final class - can not be subclassed
b) final variable - can not be modified (constant)
c) final method - can not be overridden
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final variable which is a reference to a object referent must point to the same referent, but the referent's state can change and is therefore not a constant.

Also a final method cannot be overiddem but can be over-loaded.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

fianl keyword specifies something that the definition dont be changed. if it is a variable, the value wont be changed, for a method, it wont overriden and for a class, it wont inherit. upon inherit, the properties and methods may change.

in case of a private method to be final, there is no specific meaning. because, final method is the one cant be overrided. the private class itself cant be overrided.

The only differnce is that, when a subclass given definition to a private final method in its super class, it will compile fine(Because the methods dont have a relation- they are just two independednt methods). but when you give a definition for a final method(not private), ot will give a compilation error- "Cannot Override a final method"
 
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic