Java Access Modifiers – II

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.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top