1

What is the benefit of using public private protected keywords in method declarations?

I can imagine the benefit of public private protected keywords for variables because it will prevent unnecessary "value change" by objects outside the particular class

but I cannot imagine the benefit of putting the keyword "private" or "public" or "protected" in method declarations.

If there is a method, it is my belief that the only object(s) that can use the method is the object that contains that method anyways?

user2077356
  • 37
  • 1
  • 4

3 Answers3

4

First of all, let's clarify the meaning of public, protected, package, and private access. In each case, you are correct that an object must exist to use a non-static method. However, these keywords indicate who is allowed to invoke the method on the object:

  • public methods can be invoked by anyone.
  • protected methods can be invoked by any class in the same package or subclasses in other packages.
  • package private methods (without any access modifier) can be only invoked by classes in the same package.
  • private methods can be invoked only by the same class.

As an example, I often use private methods to factor out code which is otherwise repeated among several different methods. This code isn't part of the public interface for other classes to use. Rather, it is a "worker" method that does some of the calculations internal to the class itself.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2

public means the method can be override, and is available for use by any code for which the enclosing class is visible.

private means the method is usable only by the enclosing class.

protected means that the method is callable only by the enclosing class, and any class that extends that class.

package (no keyword before the method) means that the method can be called by any code within a class in the same package as the enclosing class.

In Java, difference between default, public, protected, and private

These different keywords are useful in designing how your class will be used. public methods are part of the "contract" you are exposing to the users of your class. private methods are typically implementation code that should not be exposed in any way to the outside world because you want to hide that logic in case you want to replace or modify it in the future without breaking your "contract" with the class' users.

protected methods are available to classes that extend your class. So maybe there's some implementation that you want to make available for overriding -- or perhaps you need an extending class to implement this method in order to make your class work, but it's not part of the "contract" your exposing to callers of your class or those calling the extending class.

Use package to create implementation code that other classes in the package can call, but that you don't want to expose to those using your class (not part of your "contract" with your users.) I use package level methods if I need to test some tricky implementation code from a unit test, but don't want to make the method public.

Community
  • 1
  • 1
Marvo
  • 17,845
  • 8
  • 50
  • 74
2

To understand the benefit of restricting access to a method, consider this : You can safely change or even remove a private method from a class, because you know it's only ever accessed from within that class. If you are publishing an API, and you expose a public method, then its out there for good. If you try to change it, you will break code that relys on it being available. You have to think about how code will evolve over time to fully appreciate this.

If there is a method, it is my belief that the only object(s) that can use the method is the object that contains that method anyways?

No, not true. Methods can be declared static, which means they do not require an object to be invoked on.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124