Java Access Modifiers
There are four types of access specifiers are available in java programming which are hide the information of class variables, constructor and methods in outside of class. The role of access specifiers are it will control usability of class properties in different scenario.
default | public | private | protected | |
---|---|---|---|---|
Same Class | Yes | Yes | Yes | Yes |
Nested Class | Yes | Yes | Yes | Yes |
Inherit Class | Yes | Yes | No | Yes |
Same Package Subclass or Non-Subclass | Yes | Yes | No | Yes |
Different Package Subclass | No | Yes | No | Yes |
Different Package Non-Subclass | No | Yes | No | No |
Default
When there are not use any keyword to define class properties and methods the compiler are provide default behaviour. default instance variable of class are publicly used by within the class or inherit class. But it will not used by within the different Package subclass or non-subclasses. For example.
//Define packge
package com.regularcode;
//Default access specifier work on class
class Package{
public void test(){
System.out.println("Test Method");
}
}
/*
File Name : Package.java
Compile by : javac -d . Package.java
*/
When compile this source code this code are not produce any error or warning. But in this Package class are not use any access modifier. Then in this situation this class are have default access specifier.
When use this package class to another java source file it will produce an error. See this example.
//Import Package
import com.regularcode.Package;
class Result{
public static void main(String[] args) {
Package obj = new Package();
obj.test();
}
}
Result.java:2: error: Package is not public in com.regularcode; cannot be accessed from outside package
import com.regularcode.Package;
^
Result.java:8: error: cannot access Package
Package obj = new Package();
^
bad source file: ./Package.java
file does not contain class Package
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
2 errors
When we are modified public class Package in com.regularcode package and recompile Package class then it will work. Similar way default method of package is cannot be accessible of within imported class.
Public
public methods and variable are available within the defining class and implement objects of class. And there can also use this type of method and variable inherited class.
Protected
Protected is an intermediate modifier of public and protected specifiers. That are accessible inside on class or within the inherit child class methods.
Private
Private is highly secure level, defining methods and class variable as private then its will use only within of defining class. There are not use by within the inherited class method.
class Info{
//private variable
private int mark;
//Constructor of class
public Info(int mark){
this.mark=mark;
}
public int dataInfo(){
//Ok return private variable value
return mark;
}
}
class Test{
public static void main(String[] args) {
//Create a object
Info obj = new Info(100);
System.out.println("Mark :" +obj.mark); //not work
System.out.println("Mark :" +obj.dataInfo()); //it will work
}
}
Test.java:15: error: mark has private access in Info
System.out.println("Mark :" +obj.mark); //not work
^
1 error
Note that in case main method are defined within Info class then this program are not produce any error. Because private variable of class are accessible within the class but in this case main() method are define within Test method then there are not possible to access the private variable of Info class in outside of this class.
So we can use private variable of class using public method. In this case dataInfo() method are returning value of private data.
class Info{
//private variable
private int mark;
//Constructor of class
public Info(int mark){
this.mark=mark;
}
public int dataInfo(){
//Ok return private variable value
return mark;
}
}
class Test{
public static void main(String[] args) {
//Create a object
Info obj = new Info(100);
//System.out.println("Mark :" +obj.mark); //not work
System.out.println("Mark :" +obj.dataInfo()); //it will work
}
}

Mark :100
Note that private instance variable are provide Encapsulation in java programming. And similar way private methods are not execute by class object this method are call inside class function.
public class Authentication{
private String id;
public String name;
private double salary;
private String password;
Authentication(String id,
String name){
this.id=id;
this.name=name;
this.salary=59000; //starting salary
this.password="X******";//Encrypted
}
private boolean checkUser(String id){
//Logically Fetching user record using id and check
//that user are belong to particular access
//When user are active and there are permission to login
return true; //otherwise return false
}
private boolean checkPassward(){
//Here check that given encrypted password are valid or not
//When user password are valid
return true; //otherwise return false
}
public boolean doLogin(){
//Accessing private method inside a function
if( checkPassward() && checkUser(this.id)){
return true;
}
else{
return false;
}
}
public static void main(String[] args) {
Authentication person = new Authentication("007","Foo");
boolean status=person.doLogin();
if(status==true){
System.out.println("Welcome");
}else{
System.out.println("Something wrong");
}
}
}

Welcome
In this example there are two private method checkPassward() and checkUser() which is accessed within the class function. Note that each method (public,pivate protected) in same class can executes any function.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment