Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java access modifiers - method available to subclasses and package

Which access modifiers which when used with the method makes it available to all the class and subclasses within a package?

like image 769
Abhishek Sanghvi Avatar asked Apr 09 '26 11:04

Abhishek Sanghvi


1 Answers

public, protected and the default modifier (which doesn't have a keyword). Everything except private.

For example, suppose the package foo has the following class:

public class MyClass {
   public void method1() { };
   protected void method2() { };
   void method3() { };
   private void method4() { };
}

Then a class foo.SecondClass could call the methods method1, method2 and method3, but not method4.

See the Java tutorial for a useful table of what each modifier allows.

like image 86
Simon Nickerson Avatar answered Apr 11 '26 01:04

Simon Nickerson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!