The concept of ‘access modifiers’, understanding them and working with them are some of the key objectives of the SCJP/OCJP exam. It is also important to understand this concept to excel as a Java developer as well. We have already started our discussion of ‘Access modifiers’ in our earlier post. We will continue our discussion by extending it to ‘private access modifier’.
‘private modifier’:
Only methods and variables can use the ‘private access’ modifier’. The ‘private access modifier’ is the most restrictive access modifier.
One of the interesting ways to think about the ‘private access modifier’ is to view it as “invisible”. If the variables and methods are declared “private”, they are “invisible” and hence they cannot be accessed in any other class or sub-classes. This brings us to the first of the three key points that we will discuss:
1. ‘private’ variables and methods can only be accessed in the class where it was declared.
As an example, consider this program:
example3.java:
package whizlabs;
public class example3 {
private int i,j;
private void add_1(){
System.out.println(“Hello world”);
}
public static void main(String[] args) {
}
}
class example4{
example3 e=new example3();
e.add_1();
}
The above code will cause a compiler error since add_1() is being accessed in another class other than the class where it was declared. This will be the output on the console.
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
at whizlabs.example3.main(example3.java:9)
Declaring it this way will rectify the error:
package whizlabs;
public class example3 {
private int i,j;
private void add_1(){
System.out.println(“Hello world”);
}
public static void main(String[] args) {
example3 e=new example3(); //code has been moved here
e.add_1();
}
}
class example4{
// An empty class declaration
}
2. The “invisible” private methods and variables will not be inherited by the sub-classes.
Consider this code: example3.java:
package whizlabs;
public class example3 {
private int i,j;
private void add_1(){
System.out.println(“Hello world”);
}
public static void main(String[] args) {
example3 e=new example3();
e.add_1();
}
}
Next consider this code: example5.java:
package whizlabs;
public class example5 extends example3 {
public void test(){
add_1();
}
public static void main(String[] args) {
}
}
example5.java is a sub-class of example3.java and both of them are in the same package ‘whizlabs’.
Even though example5.java is a sub-class of example3.java, method add_1() is “private” and it will be invisible in ‘example5.java’.
This will be the error that will be encountered:
“The method add_1() from the type example3 is not visible”
Changing the visibility from ‘private’ to ‘public’ for the add_1() method in ‘example3.java’, will eliminate this error.
public void add_1(){
System.out.println(“Hello world”);
}
3. ‘private’ methods cannot be overridden by inherited sub-classes.
Put simply, if a method is “invisible” (private) how can it be overridden by sub-classes? If a private method is declared with the same signature as its super-class, it is just considered as “another method” and not as an overridden method.
Consider the code below: example3.java:
package whizlabs;
public class example3 {
private int i,j;
private void add_1(){
System.out.println(“Hello world”);
}
public static void main(String[] args) {
}
}
example5.java:
package whizlabs;
public class example5 extends example3 {
private void add_1();
System.out.println(“Hello!!”);
}
public static void main(String[] args) {
example5 e=new example5();
e.add_1();
}
}
This will give us the following output on the console:
Hello!!
In the above example, we can see that even though the method add_1() has been defined again in ‘example5.java’ – it is not overriding. It is just considered as another method and the result “Hello!!” is printed out.
We have seen the ‘private’ access modifiers and some key points related to it in this post. We will extend our discussion of the access modifiers in the next post.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021