Java

Java Access Modifiers

While learning how to write Java programs, you must have come across the phrases public, private, and protected. These words are access modifiers. Access to a class, constructor, data member, and function in another class is restricted by an access modifier. There are four access modifiers in Java:

1. default
2. private
3. protected
4. public

 

1. Default access modifier

The term “default access modifier” refers to the absence of any access modifier. This modifier’s use is restricted to the package alone. This indicates that only classes that are in the package in which the class with the default access modifier resides may access it. This class is inaccessible to any other class outside of this package. Similar to how a default method or data member in one package’s class would not be visible in another package’s class. Here is an illustration to help you understand:

Default Access Modifier Example in Java

The default method of the Addition class is being attempted to be accessed by the Test class, which is a member of a separate package. As a result, this program would fail to compile since the default modifier’s scope is only applicable to the package in which it is defined.

Addition.java

package abcpackage;

public class Addition {
   /* Since we didn't mention any access modifier here, it would
    * be considered as default.
    */
   int addTwoNumbers(int a, int b){
	return a+b;
   }
}

Test.java

package xyzpackage;

/* We are importing the abcpackage
 * but still we will get error because the
 * class we are trying to use has default access
 * modifier.
 */
import abcpackage.*;
public class Test {
   public static void main(String args[]){
	Addition obj = new Addition();
        /* It will throw error because we are trying to access
         * the default method in another package
         */
	obj.addTwoNumbers(10, 21);
   }
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method addTwoNumbers(int, int) from the type Addition is not visible
at xyzpackage.Test.main(Test.java:12)

2. Private access modifier

Private modifiers may only be used inside a class.

  • Class and Interface classes and interfaces cannot be marked as private.
  • Private Data members and functions are only available inside the class.
  • You cannot create an object of a class from outside the class if it has a private constructor.
    To further grasp this, let’s look at an example:

Private access modifier example in java

Because we are attempting to access the private data member and method of class ABC in the class Example, this example raises a compilation error. Only other members of the class may access the private data member and function.

class ABC{  
   private double num = 100;
   private int square(int a){
	return a*a;
   }
}  
public class Example{
   public static void main(String args[]){  
	ABC obj = new ABC();  
	System.out.println(obj.num); 
	System.out.println(obj.square(10));
   }  
}

Output:

Compile - time error

3. Protected Access Modifier

Only classes belonging to the same package and its subclasses have access to protected data members and methods. With the exception of its accessibility in sub classes, the protected access modifier may similarly be compared to the default access modifier.
Declaring classes protected is not possible. It is typical to apply this access modifier in parent-child relationships.

Protected access modifier example in Java

In this example the class Test which is present in another package is able to call the addTwoNumbers() method, which is declared protected. This is because the Test class extends class Addition and the protected modifier allows the access of protected members in subclasses (in any packages).

Addition.java

package abcpackage;
public class Addition {

   protected int addTwoNumbers(int a, int b){
	return a+b;
   }
}

Test.java

package xyzpackage;
import abcpackage.*;
class Test extends Addition{
   public static void main(String args[]){
	Test obj = new Test();
	System.out.println(obj.addTwoNumbers(11, 22));
   }
}

Output:

33

4. Public access modifier

Anywhere in the world may access the members, methods, and classes that are declared public. This modification does not impose any access limitations.

public access modifier example in java

Let’s use the same example as before, but this time the method addTwoNumbers() has a public modifier, making it accessible to class Test even if it doesn’t extend the Addition class. This is so that everyone may see the public modifier.

package abcpackage;

public class Addition {

   public int addTwoNumbers(int a, int b){
	return a+b;
   }
}

Test.java

package xyzpackage;
import abcpackage.*;
class Test{
   public static void main(String args[]){
      Addition obj = new Addition();
      System.out.println(obj.addTwoNumbers(100, 1));
   }
}

Output:

101
package abcpackage;

public class Addition {

   public int addTwoNumbers(int a, int b){
	return a+b;
   }
}

Test.java

package xyzpackage;
import abcpackage.*;
class Test{
   public static void main(String args[]){
      Addition obj = new Addition();
      System.out.println(obj.addTwoNumbers(100, 1));
   }
}

Output:

101

Let’s look at a table to examine the range of these access modifiers:

------------+-------+---------+--------------+--------------+--------
            | Class | Package | Subclass     | Subclass     |Outside|
            |       |         |(same package)|(diff package)|Class  |
————————————+———————+—————————+——————————----+—————————----—+————————
public      | Yes   |  Yes    |    Yes       |    Yes       |   Yes |    
————————————+———————+—————————+—————————----—+—————————----—+————————
protected   | Yes   |  Yes    |    Yes       |    Yes       |   No  |    
————————————+———————+—————————+————————----——+————————----——+————————
default     | Yes   |  Yes    |    Yes       |    No        |   No  |
————————————+———————+—————————+————————----——+————————----——+————————
private     | Yes   |  No     |    No        |    No        |   No  |
------------+-------+---------+--------------+--------------+--------

Related Articles

Leave a Reply

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

Back to top button