There are two modifiers in Java – access modifiers and non-access modifiers. In this post, we will discuss the access modifiers in Java.
There are four types of access modifiers in Java –
- Public Access Modifier
- Private Access Modifier
- Protected Access Modifier
- Default Access Modifier or No modifier
The below table describes their access through packages and classes in a nutshell.
Private | Default( or No modifier) | Protected | Public | |
---|---|---|---|---|
Same class | Yes | Yes | Yes | Yes |
Same package subclass | No | Yes | Yes | Yes |
Same package non-subclass | No | Yes | Yes | Yes |
Different package subclass | No | No | Yes | Yes |
Different package non-subclass | No | No | No | Yes |
So, let’s start with each of the access modifiers.
Public Access Modifier
- This has the widest scope among all Java access modifiers and is accessible from anywhere.
- So, when a method or variable member is declared public, all other classes can access the members regardless of the package they belong to ( assuming the class itself is visible).
Let us see with an example now, where we will make two classes ( Alpha and Beta ) in two different packages.
Alpha class in Package A
package A;
public class Alpha {
public int alphaVariable ;
public void show() {
System.out.println("alphaVariable = "+alphaVariable);
}
}
Beta class in package B
package B;
import A.Alpha;
public class Beta {
public int betaVariable;
public static void main(String[] args) {
Alpha alpha = new Alpha();
alpha.alphaVariable = 5; // accessing the member of "Alpha" class from "Beta" class
alpha.show();
}
}
The output of the code after running the main method –
alphaVariable = 5
As members of the Alpha class have public access modifier, we could access them from the Beta class. Even though the Beta class is in a different package. And this is not limited to packages; we can access the public members of a class across multiple projects too.
Private Access Modifier
- This has the most limited scope of all the access modifiers in Java.
- So, when a method or variable member is declared private, then it can be accessed only from within that class and not by any other class or subclass.
Let’s again take classes ( Alpha and Beta) and assign a private access modifier to the member of the Alpha class.
Alpha.java
public class Alpha {
private int alphaVariable ; // made this variable "private"
public void show() {
System.out.println("alphaVariable = "+alphaVariable);
}
}
Beta.java
public class Beta {
public int betaVariable;
public static void main(String[] args) {
Alpha alpha = new Alpha();
alpha.alphaVariable = 50; // accessing the member of "Alpha" class from "Beta" class
alpha.show();
}
}
Here code will give you the below error –
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Alpha.alphaVariable is not visible
It clearly says that the field we made private is not visible. So, we won’t be able to access it outside the class. But if we access the alphaVariable in the Alpha class itself instead of at the Beta class, then it will work fine as the class which encompasses the private member can access it.
Alpha.java
public class Alpha {
private int alphaVariable; // made this variable "private"
public void show() {
alphaVariable = 5;
System.out.println("alphaVariable = " + alphaVariable);
}
}
Beta.java
public class Beta {
public int betaVariable;
public static void main(String[] args) {
Alpha alpha = new Alpha();
alpha.show();
}
}
The output of the code after running the main method –
alphaVariable = 5
See, all was good at the end 🙂
Protected Access Modifier
- This is the most misunderstood of all the access modifiers, but it is relatively simple to understand, which you will see soon.
- It has a scope between public access modifier and private access modifier.
We will understand the protected modifier with the help of different scenarios –
Scenario 1 –
Let’s think of two classes ( Alpha and Gamma) in the same package. One of the members of the Alpha class is declared protected.
Will we be able to access that member of the Alpha class from the Gamma class?
The answer is Yes. We can access the protected member of the Alpha class within the same package through any other class. It doesn’t matter whether it is a subclass of the Alpha class. We can think of it as a public member; only the classes accessing the protected variable should belong to the same package.
Alpha class in package A
package A;
public class Alpha {
protected int alphaVariable; // this variable is declared "protected"
public int secondVariable;
}
Gamma class is also in package A
package A;
public class Gamma {
private int gammaVariable;
public static void main(String[] args) {
Alpha alpha = new Alpha();
alpha.alphaVariable = 100;
System.out.println("alphaVariable = " + alpha.alphaVariable);
}
}
So, just run the Gamma class as a simple java application and see what happens. You will be happy to see that the program works fine 😛
Output –
alphaVariable = 100
Scenario 2 –
Let’s again think of two classes ( Alpha and Beta), but both will be in different packages this time. Again one of the members of the Alpha class is declared protected, and now we have to see whether we will be able to access it from the Beta class or not.
Now again, there will be two scenarios in this case –
- The beta class is the subclass of the Alpha class.
- And Beta class is not the subclass of the Alpha class, which means it can be any other class.
In the first scenario, we will be able to access that protected member of the Alpha class, while in the second scenario, we won’t be able to do that. Let’s see both of the scenarios with the help of examples.
Alpha class in package A
package A;
public class Alpha {
protected int alphaVariable; // this variable is declared "protected"
}
Beta class in package B ( and not a subclass of Alpha)
package B;
import A.Alpha;
public class Beta {
public int betaVariable;
public static void main(String[] args) {
Alpha alpha = new Alpha();
alpha.alphaVariable = 100;
}
}
Now, this will throw a compilation error as shown below –
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Alpha.alphaVariable is not visible
Now, let’s make one change and make the Beta class a subclass of Alpha
package B;
import A.Alpha;
public class Beta extends Alpha {
public int betaVariable;
public static void main(String[] args) {
Beta beta = new Beta();
beta.alphaVariable = 100;
System.out.println("alphaVariable = " + beta.alphaVariable);
}
}
Now, what happens here is that the protected member of a class is also inherited by its subclass. So, we can access the protected member of the Alpha class through its subclass now.
Output –
alphaVariable = 100
Note: So, in different packages, we can access the protected members of a class through inheritance only.
Default Access Modifier
When you do not write any of the above ( public, private, and protected ) access modifiers, then java automatically assumes it to be the default access modifier. Thus, below two statements will work in the same way –
int variableName;
will work in the same way as
default int variableName;
An interesting point about the default access modifier is that we won’t be able to access the default members of a class outside the package. Thus, the default access modifier has package-level access.
Scenario 1 –
Let’s take two classes ( Alpha and Gamma) in the same package. We will not write any access modifiers on the Alpha class variable and then try to access it from the Gamma class.
Alpha class in package A
package A;
public class Alpha {
int alphaVariable; // this will be considered as "default"
}
Gamma class also in package A
package A;
public class Gamma {
private int gammaVariable;
public static void main(String[] args) {
Alpha alpha = new Alpha();
alpha.alphaVariable = 100;
System.out.println("alphaVariable = " + alpha.alphaVariable);
}
}
This code will compile fine. So, within a package, the default access modifier will act like the public access modifier.
Output after running Gamma class –
alphaVariable = 100
Scenario 2 –
We will again take two classes ( Alpha and Beta), but this time, both will be in different packages and then try to do the same thing as we did above.
Alpha class in package A
package A;
public class Alpha {
int alphaVariable; // this will be considered as "default"
}
Beta class in package B
package B;
import A.Alpha;
public class Beta {
private int gammaVariable;
public static void main(String[] args) {
Alpha alpha = new Alpha();
alpha.alphaVariable = 100; // will through compilation error
}
}
This time, it will throw a compilation error.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Alpha.alphaVariable is not visible
So, what do we learn from here? The default members will have package-level access and won’t be accessible outside their class’s package.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to reach us in the comments or mail us at admin@codekru.com.