aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private final method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private final method" Watch "private final method" New topic
Author

private final method

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, How private final makes sense here.. What I mean is, What is the difference between private final and just private?? Subclass cannot see this method in any case.
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

final means you can't override this method.

and declaring a method private means it wouldn't be seen by its subclasses.if it is not visible means it can't be overriden.

so,declaring a private method final is redundant.
because declaring a method final means you are saying to compiler don't override this method.but declaring it private also means the same implicitly.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You can still override private methods, if you define an extending class within the same class. Final would in this case prevent the method from being overridden by a subclass within the same class.

The use of this is still questionable though. Private final just doesn't make much sense. I was just pointing out that it's not necessarily redundant.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Stephan van Hulst wrote:You can still override private methods, if you define an extending class within the same class. Final would in this case prevent the method from being overridden by a subclass within the same class.

The use of this is still questionable though. Private final just doesn't make much sense. I was just pointing out that it's not necessarily redundant.


I just tried a sample code:



Output:


When I tried to remove the private from the method hello() in MyClass and compile-




Mohamed Sanaulla | My Blog
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
mohammed sanaullah wrote:
Stephan van Hulst wrote:You can still override private methods, if you define an extending class within the same class. Final would in this case prevent the method from being overridden by a subclass within the same class.

The use of this is still questionable though. Private final just doesn't make much sense. I was just pointing out that it's not necessarily redundant.


I just tried a sample code:



Output:





The above code is not overriding. I have changed the code with superclass reference and subclass object.


Now the output is Hello
Hello2
which is not the case of overriding.


Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Stephan, Your explanation is awesome. Thanks to Mohammed and Bhanu for proving code snippets.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Mohammed and Bhanu, I stand corrected.

Private methods can indeed not be overriden, not even in an internal class. Private final is always redundant.
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Stephan, Earlier you mentioned "You can still override private methods," Here, you said not be overriden. Please confirm the right one. If you think this code is incorrect, Could you Please provide some code on how to use this concept, so that we can understand properly.
Stephan van Hulst wrote:Mohammed and Bhanu, I stand corrected.

Private methods can indeed not be overriden, not even in an internal class. Private final is always redundant.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

My last post is the correct one. My view in the first post was corrected by Mohammed and Bhanu.

You asked if there's a difference between private and private final methods. No, there isn't. private final is redundant. Always use private only.
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

On private methods,no overriding rules and no runtime polymorphism rules applies.It is in the explanation of the examlab question.

so,declaring private with final is redundant.
 
jQuery in Action, 2nd edition
 
subject: private final method