File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why final keyword is accepted with private? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why final keyword is accepted with private?" Watch "Why final keyword is accepted with private?" New topic
Author

Why final keyword is accepted with private?

Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
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.)


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
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.


Regards,<br />Seb<br /> <br />SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
How about inner classes?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why final keyword is accepted with private?
 
Similar Threads
native method
the" fina"l and "static'
question about abatract
Abstract methods
final & constructors