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.