Access modifiers in Java are used to specify access levels for classes, variable methods and constructor.
default: The scope of default access modifier is limited to the package only. If we do not mention any access modifier, then it acts like a default access modifier.
Let’s see a practical example to understand this better.
1 2 3 4 5 6 7 8 9 10 |
package packageOneDefault; public class DefaultClassOne { // Here I didnt mention any modifier so it acts as a default modifier. protected int myMethod(int x){ return x; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package packageTwoDefault; import packageOneDefault.DefaultClassOne; public class DefaultClassTwo { public static void main(String args[]){ // Created an object of ClassOne DefaultClassOne obj = new DefaultClassOne(); // Trying to access the default method of ClassOne which is in packageOne // You can find an error at obj.myMethod obj.myMethod(10); } } |
private: The scope of private access modifier is only within the classes.
Note: Class or Interface cannot be declared as private
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package packageThreePrivate; public class PrivateClassOne { private int x = 100; int y = 200; private int myMethod(int a){ return a; } int myMethodOne(int a){ return a; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package packageThreePrivate; public class PrivateClassTwo { public static void main(String args[]){ PrivateClassOne obj = new PrivateClassOne(); // Here I am trying to access the private data member and private method of ClassOne // It throws compilation error System.out.println(obj.x); System.out.println(obj.myMethod(1000)); // Here I am trying to access default data member and default method of ClassOne // No error. We can comfortable access default modifier with in the package System.out.println(obj.y); System.out.println(obj.myMethodOne(2000)); } } |
protected: The scope of protected access modifier is within a package and also outside the package through inheritance only.
Note: Class cannot be declared as protected
1 2 3 4 5 6 7 8 9 |
package packageFourProtected; public class ProtectedClassFour { protected int myMethod(int a){ return a; } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
package packageFiveProtected; import packageFourProtected.ProtectedClassFour; public class ProtectedClassFive extends ProtectedClassFour{ public static void main(String args[]){ ProtectedClassFive obj = new ProtectedClassFive(); System.out.println(obj.myMethod(100)); } } |
public: The scope of public access modifier is everywhere. It has no restrictions. Data members, methods and classes that declared public can be accessed from anywhere.
1 2 3 4 5 6 7 8 9 10 |
package packageSixPublic; public class PublicClassSix { // Here I didnt mention any modifier so it acts as a default modifier. public int myMethod(int x){ return x; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package packageSevenPublic; import packageSixPublic.PublicClassSix; public class PublicClassSeven { public static void main(String args[]){ // Created an object of ClassOne PublicClassSix obj = new PublicClassSix(); // Trying to access the public method of PublicClassSix which is in packageSixPublic obj.myMethod(10); // We tried the same example with Default and we faced an error. Hope you remember } } |
See this simple table to understand access modifiers easily
Must Read: Java Tutorial