wood burning stoves*
The moose likes Java in General and the fly likes Overriding private & final methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding private & final methods" Watch "Overriding private & final methods" New topic
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: 4181
    
  21

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: 38515
    
  23
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: 4181
    
  21

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: 4181
    
  21

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

Joined: Aug 16, 2005
Posts: 14117
    
  16

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: 3013
    
  10
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: 38515
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding private & final methods