• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

private final method

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 15743
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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-



 
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan, Your explanation is awesome. Thanks to Mohammed and Bhanu for proving code snippets.
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic