• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private method

 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is private method implicitly final?
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for making it clear
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private method indeed is implicitly final Have a lok at thsi thread for more details.
HTH,
- Manish
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic