aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

Dee Irun
Greenhorn

Joined: Nov 13, 2004
Posts: 19
"A method marked private and/or final cannot be overidden in a subclass". I have few questions regarding this statement. Let's say I have a method doNotOverride() in my superclass which is marked private and final. Then, I write a method with the same signature, except it is not final in my subclass. Well, this compiles fine, and as I understand, it is not an overridden method, rather just a method in subclass with the same signature. Now, if I have the same method doNotOverride() in my superclass marked public and final instead, and have a method with the same signature in my subclass, I get this compiler error saying "cannot override method. overridden method is final". I still am trying to do the same thing as I did in the previous case, not overriding, but having a method with the same signature in subclass. Why did the compiler complain in the latter case (public final) and not in the former (private final)? Any replies are appreciated. Thanks!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
See if this helps answer your question:



Now change the access modifier on the printS method.
Private methods aren't overridden, they are inherited (although the JLS uses this term ambiguously).


Tony Morris
Java Q&A (FAQ, Trivia)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The difference is that a private superclass method, final or not, is not inherited by a subclass. It is not even visible to the subclass.

Your subclass can have a method with a matching signature but perhaps with a different return type and throwing an extra exception. No problem. You are not overriding or overloading the private superclass method, you are ignoring it.

The only reason the subclass method cannot also have a narrower access modifier is that there is nothing narrower than private.


Mike Gershman
SCJP 1.4, SCWCD in process
Subhash Bhushan C
Ranch Hand

Joined: Jan 27, 2005
Posts: 106
Private methods aren't overridden, they are inherited (although the JLS uses this term ambiguously).

Private methods aren't even inherited. They are born and they die with the class they are declared in. They are not accessible anywhere in the world other than in the class.

When it comes to overriding a private or final method, all that matters is visibility. If the subclass can see the method (meaning able to access the method), then if it wants to define its own version of the method, it will have to follow the rules of overriding, which are:
  • The argument list must exactly match that of the overridden method.
  • The return type must exactly match that of the overridden method.
  • The access level must not be more restrictive than that of the overridden method.
  • The overriding method must not throw new or broader checked exceptions than those declared by the overridden method.

  • But if the method is not visible, then its similar to having 2 different methods, one in the superclass and another in the subclass, which do not have any kind of relation with each other, but (surprisingly) which happen to have the same method name and signature.


    Regards,<br />Subhash Bhushan.
    Dee Irun
    Greenhorn

    Joined: Nov 13, 2004
    Posts: 19
    Thanks all for your replies!
    Tony Morris
    Ranch Hand

    Joined: Sep 24, 2003
    Posts: 1608

    Private methods aren't even inherited.


    That depends on which part of the JLS that you are reading, and also on the exact definition of inheritance (not just how the JLS sees it). This argument has been had, and I'm not too keen on starting it all up again, suffice to say it boils down to the definition of "inheritance".


    They are born and they die with the class they are declared in. They are not accessible anywhere in the world other than in the class.


    Almost. By your definition, the following is impossible:
    Subhash Bhushan C
    Ranch Hand

    Joined: Jan 27, 2005
    Posts: 106
    This argument has been had, and I'm not too keen on starting it all up again, suffice to say it boils down to the definition of "inheritance".

    Can you please provide a link? I am ignorant about this.

    Almost. By your definition, the following is impossible:

    I was concentrating about accessibility of private methods in subclasses, not inner class private member visibility in other member classes. I agree to what you are saying.
    [ February 08, 2005: Message edited by: Agni Vartula ]
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Overriding
     
    Similar Threads
    Overidden Method
    Private method overriden...
    overridding private methods
    khalid mughal