4

What exactly is the purpose of having private access modifier for the methods in Java if they are prohibited for the modification by the final modifier as well?

And The Java Language Specification explicitly notes that:

A method can be declared final to prevent subclasses from overriding or hiding it.

Then why not to simply mark a method as final?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 3
    Because private is less visible than non-private? Visibility modifiers have a different purpose than the `final` modifier, it just so happens that private methods cannot be overridden by virtue of them being private. – Slaw Jul 15 '20 at 20:07
  • 1
    Does this answer your question? [Why private method can not be final as well?](https://stackoverflow.com/questions/23161362/why-private-method-can-not-be-final-as-well) – user Jul 15 '20 at 20:07
  • It totally makes sense for fields to be `private final`, but yeah, if a method is `private`, then `final` shouldn't be needed. – user Jul 15 '20 at 20:09
  • Final also prevents modification by the object itself, so final and private can make sense. – NomadMaker Jul 15 '20 at 20:24
  • 1
    @NomadMaker But that doesn't hold for methods, which the OP's asking about. – user Jul 15 '20 at 20:32
  • @user That question asks a different question than this one. – Mark Rotteveel Jul 18 '20 at 18:13
  • @MarkRotteveel The question title is misleading, but it does ask pretty much the same question. "Is it redundant to add private and final to a same method?" – user Jul 18 '20 at 18:16
  • 1
    @user No, the OP of this question seems to think final and private do the same thing, while the other question simply asks why you can specify `final` for private methods, even though it is redundant. – Mark Rotteveel Jul 18 '20 at 18:19
  • @MarkRotteveel Now that you mention it, it does seem like the OP doesn't understand that private limits visibility and making the method effectively final is just a side effect. Still, I'm going to keep the close vote unless the OP clarifies their question since the two are similar enough, and if the OP doesn't understand what `private` does, they can always research it themselves. – user Jul 18 '20 at 18:22

2 Answers2

3

The private modifier and final keyword are different things and have different purposes.

Private is an access modifier. Only code in the same class can call private methods or access private fields. Final is a keyword used to define an entity that can only be assigned once.

A final field can not have its value changed, ever. But it can be read by any other class. So, if you do not want it to be read by other classes, then you can set it as private field.

A private field can have its value change, but not by other classes, only by the class it belongs. If you do not want it to be changed, ever, then you can set it as a final field.

The same goes to methods. A private method is not visible to others classes, that's why it can't be overridden. The main goal here is to prevent access by others classes.

A final method can't be overridden, but it is visible to other classes.

I think that's the main difference.

  • 2
    Not a bad answer, but I think the question is more about methods than fields. You're right that private and final are different when it comes to fields, but if a method is private, then it's practically final too – user Jul 15 '20 at 22:25
1

The difference is in how code maintenance is impacted.

Private methods are invisible to the subclasses, they are not part of the interface exposed. That means the superclass can change those methods freely without worrying about impacting code that subclasses it. At some point the private methods are called by more public ones that affect the subclasses, but the more you keep private the more scope you have to move code around.

If you make the method final but not private, then subclasses can call it directly, and you have to make sure any changes you need to make to them don't cause breakage in the subclasses.

If your class is part of a project used by others, you can't know who is using what methods of your superclass, so you can get into a situation where you can't safely change the behavior of anything once it's exposed. Better to keep as much private as possible.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Another way of saying this is that declaring a method as `private` prevents certain kinds of unwanted [coupling](https://en.wikipedia.org/wiki/Coupling_(computer_programming)) between the class and (possible) subclasses. – Stephen C Nov 14 '21 at 04:43