| Author |
Overriding private & final methods
|
Debojit Sinha
Ranch Hand
Joined: Mar 13, 2010
Posts: 66
|
|
|
Purely from a method overriding point of view, what is the difference between a private and final method?
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
Debojit Sinha wrote:Purely from a method overriding point of view, what is the difference between a private and final method?
From the JLS:
"8.4.3.3 final Methods
A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.
A private method and all methods declared immediately within a final class (ยง8.1.1.2) behave as if they are final, since it is impossible to override them."
From that quote, I would say that from a method overriding point of view - there is no difference between the two.
See: http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
|
Steve
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
You can't override a private method. You can't override a final method. But you can create another method with the same signature as a superclass private method. That is not overriding; not quite sure what it is called.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
Campbell Ritchie wrote:... But you can create another method with the same signature as a superclass private method. That is not overriding; not quite sure what it is called.
That is hiding (also called shadowing). You can't hide final methods (that are visible to subclasses), but you can hide private methods.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Steve Luke wrote: You can't hide final methods (that are visible to subclasses)
if the final method is not a private
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
Seetharaman Venkatasamy wrote:
Steve Luke wrote: You can't hide final methods (that are visible to subclasses)
if the final method is not a private 
<EDIT>
Woops, I read your message backwards. The below shows what you said is correct.
</EDIT>
Actually, you can hide private final methods (if they are visible to the subclass):
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12926
|
|
Steve Luke wrote:That is hiding (also called shadowing). You can't hide final methods (that are visible to subclasses), but you can hide private methods.
But private methods are already not visible in subclasses, so I'm not sure whether you should call it "hiding" in this case.
You can add a method to a class that happens to have the same method signature as a private method in a superclass, but that method really doesn't have anything to do with the method in the superclass - it's a completely separate method.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
I would submit that since private methods are invisible outside of the class,
there is nothing to hide or override. From the perspective of any other class,
the private member doesn't even exist. So a similar looking method has no
relationship to the private one and doesn't need a term to describe it. Even
a hidden member can be accessed from another class.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
|
Obviously, Jim and Jesper's comments do not apply to the last example Steve gave - because private members are visible to other classes contained within the same top-level class. But they do apply to the most common case, in which no nested classes are involved, and all classes under discussion are separate top-level classes. In this case, private members are completely invisible to other classes. They can't be overridden, and they can't be hidden - two different things, but both assume that the overridden/hidden method must at least be accessible in the overriding/hiding class. Private methods in separate top-level classes can never be overridden or hidden - they're like completely independent methods.
|
 |
Debojit Sinha
Ranch Hand
Joined: Mar 13, 2010
Posts: 66
|
|
|
Okay, thanks a lot guys, this was a lot of info, I learnt a lot more than I asked and appreciate it a lot. This noob is very grateful.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
You're welcome
|
 |
 |
|
|
subject: Overriding private & final methods
|
|
|