This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding private & final methods

 
Debojit Sinha
Ranch Hand
Posts: 66
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Purely from a method overriding point of view, what is the difference between a private and final method?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic