Debojit Sinha wrote:Purely from a method overriding point of view, what is the difference between a private and final method?
From the JLS:
"18.104.22.168 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 (§22.214.171.124) 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.
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 wrote:
Steve Luke wrote: You can't hide final methods (that are visible to subclasses)
if the final method is not a private
Woops, I read your message backwards. The below shows what you said is correct.
Actually, you can hide private final methods (if they are visible to the subclass):
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.
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 ... ...