| 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.
|
 |
 |
|
|
subject: Why final keyword is accepted with private?
|
|
|