Debojit Sinha wrote:Purely from a method overriding point of view, what is the difference between a private and final method?
From the JLS:
"184.108.40.206 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 (§220.127.116.11) 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.
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 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.
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.
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.