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 private method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private method" Watch "private method" New topic
Author

private method

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Is private method implicitly final?


Sunita<br />SCJP 1.4
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

nope. However in most cases... it will sorta act like its final (you can't override it 'cause you can't see it).
BUT -- what if you have an inner class that extends the outer class -- then you'd have access to that private method and you COULD override it.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks for making it clear
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Yes, a private method is implicitly final.
A private method and all methods declared in a final class (�8.1.1.2) are implicitly final, because it is impossible to override them. It is permitted but not required for the declarations of such methods to redundantly include the final keyword. JLS 8.4.3.3

This statement in the JLS bothers me for two reasons.
(1) They forgot about hiding. �because it is impossible to override� or hide �them�.
(2) A private method is never inherited. So you can declare a method with the same signature in a subclass. However, it is a compile-time error to attempt to override or hide a final method.

[ March 26, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
A private method is never inherited. Therefore, it can never be overridden or hidden.
Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass. JLS 8.4.6.3

The private method m declared in the enclosing class is accessible to the inner class.
But the method m declared in the inner class does not override or hide the private method declared the superclass.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
A better way to show that the method m declared in the inner class does not override the private method m declared in the superclass is to declare the method m in the subclass with a different return type.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

private method indeed is implicitly final Have a lok at thsi thread for more details.
HTH,
- Manish
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Manish, I read your Sep 2001 discussion about the JLS saying that private methods are implicitly final. I have the same concerns.
But my way of coping with the inconsistency is different from Jose�s explanation.
A final method can be treated differently by the compiler. The body of a final method can be inlined. (JLS 8.4.3.3) The Java Programming Language says �The same optimizations can be applied to private and static methods, because they too cannot be overridden.� (JPL 3.6)
Therefore, I think a private method is implicitly final �in the sense� that the compiler can optimize both, but not �in the sense� that a method with the same signature can or cannot be declared in a subclass.
Lih Chang
Greenhorn

Joined: Mar 21, 2003
Posts: 19
Originally posted by Marlene Miller:
A private method is never inherited. Therefore, it can never be overridden or hidden.

I'm not quite understanding what does it mean about hiding a method. In your example, (1) if you did not specifically call super.m(), then which m() should be called. (2) it seems that the m() declared in the inner class hides the m() in the outer. Since if m() not declared in the inner, the m() can be accessed directly without the super keyword. I'm kind of confused.
Lih Chang
Greenhorn

Joined: Mar 21, 2003
Posts: 19
Sorry for the confusion. What I meant in the (1) point of my previous post is that instead of using super.m(), just call m(), then which m() will be called? the one in the inner or outer.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
>>which m() will be called?
the method declared in Inner
>>it seems that the m() declared in the inner class hides the m() in the outer
The m() declared in the inner class �shadows� the m() declared in the enclosing class.
>>Since if m() not declared in the inner, the m() can be accessed directly without the super keyword
Yes.
>>I'm kind of confused.
It�s a bizarre example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private method
 
Similar Threads
How could this be possible
Modifiers
private final method
Private Final Method
to be more public