Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding

 
Dee Irun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Subhash Bhushan C
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Dee Irun
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all for your replies!
     
    Tony Morris
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic