Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private final method

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6095
71
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-



 
bhanu chowdary
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan, Your explanation is awesome. Thanks to Mohammed and Bhanu for proving code snippets.
 
Stephan van Hulst
Bartender
Pie
Posts: 6095
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6095
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic