• 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

Why final keyword is accepted with private?

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why final keyword is accepted with private?

Private methods are not accessible outside of it's class, including to it's own subclasses. So, Overridding of a private method is not possible. Then, why Java accepts private & final kewords together in method declaration?

Note: Asking this question, because certain combination are not accepted by the java compiler like, e.g abstract & static keywords. (but don't discuss about abstract & static here, since the concepts are different in both.)
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's not a necessacity that the compiler checks for redundancies.

We can find another examples : why in interfaces can we use the keyword "public" for methods since methods are implicitly public ?

On the other hand, it's normal that the compiler complains if we declare an abstract method final, for example, because abstract methods can not be final.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about inner classes?
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

Do you think about this case ?



...compiles and prints :
A
A
AB

print() in B is not overriding print() in A.

(I had a doubt too about inner classes...)
[ October 12, 2005: Message edited by: Seb Mathe ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking along the same lines (without the final). The inner class cannot override the outer class's private method, but it can call it in the inner class's version of the method. final on the outer method is also redundant in this case. I just threw the idea in for consideration.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic