Access Modifier | Same Class | Within package | Same package by subclass | Outside package bysubclasses | Global |
---|---|---|---|---|---|
Public |
YES |
YES |
YES |
YES |
YES |
Protected |
YES |
YES |
YES |
YES |
NO |
Default |
YES |
YES |
YES |
NO |
NO |
Private |
YES |
NO |
NO |
NO |
NO |
Private access modifier can be accessible within class.
// example of private access modifier
package HELLO; class Age{ private int Age=40; private void DisplayAge(){ System.out.println("My age is"+Age); } } public class Main{ public static void main(String args[]){ Age age=new Age(); System.out.println(age.Age); //C.T Error age.DisplayAge(); //C.T Error } }
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The field Age.Age is not visible
The method DisplayAge() from the type Age is not visible
// example of Private Constructor
package HELLO; class Age{ private int Age=40; private Age() { // Private Constructor // TODO Auto-generated constructor stub } private void DisplayAge(){ System.out.println("My age is"+Age); } } public class Main{ public static void main(String args[]){ Age age=new Age(); //C.T Error as Private Constructor System.out.println(age.Age); //C.T Error age.DisplayAge(); //C.T Error } }
Output:
The constructor Age() is not visible
The field Age.Age is not visible
The method DisplayAge() from the type Age is not visible
A class, method, or data member is said to have the default access modifier by default when no access modifier is specified for it.
The data members, classes, or methods that are not declared with any access modifiers are accessible within the same package, It is like default access modifiers
package tutorial; class PackageCheck{ public void Print(){ System.out.println("Hello World"); } }
class PackageCheck is not accessible in Main.java
//Main.java
package HELLO; import tutorial.*; class Main { public static void main(String[] args){ PackageCheck packageCheck=new PackageCheck(); //C.T Error packageCheck.Print(); //C.T Error } }
The only way to access the protected access modifier is through inheritance, both inside and outside of the package.
The constructor, method, and data member can all be used with the protected access modifier. It is not permitted to be used in class.It is more accessible than the default modifier.
package tutorial; public class PackageCheck{ protected void Print(){ System.out.println("Hello World"); } }
//Main.java
package HELLO; import tutorial.*; class Main { publicstatic void main(String[] args){ PackageCheck packageCheck=new PackageCheck(); packageCheck.Print(); } }
Output:
Hello World
Everywhere has access to the public access modifier. Among all the other modifiers, it has the broadest range.
package tutorial; public class PackageCheck{ public void Print(){ System.out.println("Hello World"); } }
//Main.java
package HELLO; import tutorial.*; class Main { public static void main(String[] args){ PackageCheck packageCheck=new PackageCheck(); packageCheck.Print(); } }
Output:
Hello World
Post your comment