Access modifiers
Access modifiers are keywords used for defining the accessibility of classes, methods, and data members.
Types of access modifiers.
- Private.
- Default
- Protected
- Public
Private:
Data members, methods, and constructors that are declared with private access modifiers can be accessed into that class only.
package com.w3schools; class Student { //private members of the class private int rollNo = 5; private void showDetails(){ System.out.println("RollNo = " + rollNo); } } public class PrivateAccessModifier { public static void main(String args[]){ Student obj = new Student(); //Compile time error because private members //of class can be accessed in that class only. System.out.println(obj.rollNo); obj.showDetails(); } }
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field Student.rollNo is not visible The method showDetails() from the type Student is not visible at com.w3schools.PrivateAccessModifier.main(PrivateAccessModifier.java:19)
Note: A class can have a private constructor but an instance of that class can not be created from outside the class.
package com.w3schools; class Student { //private constructor of the class private Student(){ } public void show(){ System.out.println("Hello Wschools360.com"); } } public class PrivateConstructor { public static void main(String args[]){ //compile time error in creating Student class object //because of private constructor. Student obj = new Student(); } }
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Student() is not visible at com.w3schools.PrivateConstructor.main(PrivateConstructor.java:17)
Note: Classes and interfaces can not be private except nested classes.
Default
Classes, data members, methods, and constructors that are not declared with any access modifier are treated as default. They can be accessed into all classes within the same package only.
package com.w3schools; class Student { //Default Data members int rollNo = 50; void showRollNo(){ System.out.println("RollNo = " + rollNo); } } public class DefaultAccessModifier { public static void main(String args[]){ Student obj = new Student(); //No compile time error because default members of the class //can be accessed in that package but can't be //accessed outside the package. System.out.println(obj.rollNo); obj.showRollNo(); } }
Output
50 RollNo = 50
Default Example 2
package com.w3schools.display; class Display { void display(){ System.out.println("Hello w3schools.blog!"); } }
package com.w3schools.test; import com.w3schools.display.*; public class Test { public static void main(String args[]){ //Compile time error because default members of the class //are not accessible from outside the package. Display obj = new Display(); obj.display(); } }
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The type Display is not visible
Protected
Data members, methods, and constructors that are declared with protected access modifiers can be accessed into all classes within the same package and only in subclasses outside the package.
package com.w3schools.display; public class Display { protected void display(){ System.out.println("Hello w3schools.blog!"); } }
package com.w3schools.display; package com.w3schools.test; import com.w3schools.display.*; public class Test extends Display{ public static void main(String args[]){ Test obj = new Test(); //No compile time error because protected members //of the class are accessible from outside the //package in subclasses. obj.display(); } }
Output
Hello w3schools.blog!
Protected Example 2
package com.w3schools.display; public class Display { protected void display(){ System.out.println("Hello w3schools.blog!"); } }
package com.w3schools.test; import com.w3schools.display.*; public class Test { public static void main(String args[]){ Display obj = new Display(); //Compile time error because protected members //of the class are not accessible outside //the package in non-subclasses. obj.display(); } }
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method display() from the type Display is not visible
Note: Classes and interfaces can’t be protected except nested classes.
Public
Classes, data members, methods, and constructors that are declared with public access modifiers can be accessed everywhere.
package com.w3schools.display; public class Display { public void display(){ System.out.println("Hello w3schools.blog!"); } }
package com.w3schools.test; import com.w3schools.display.*; public class Test { public static void main(String args[]){ Display obj = new Display(); //Public class members are accessible from everywhere. obj.display(); } }
Output
Hello w3schools.blog!