Access Modifiers In Java - Java Tutorials For Selenium WebDriver

Access modifiers are the keywords in java software development language by which we can set the level of access for class, methods, variables and constructors. There are 4 different access modifiers available in java software development language. Now let me try to describe you how can we use them to set the access for the methods and variables. First of all let me tell you meaning of each access modifier keyword in java software development language and then will give you examples for them.
  • public : We can access public methods or variables from all class of same package or other package.
  • private : private methods and variables can be accessed only from same class. We can not access It from other classes or sub classes of even same package.
  • protected : protected methods can be accessed from classes of same package or sub classes of that class.
  • No Access Modifier : If method have not any access modifier then we can access It Inside all class of same package only.
Now let me show you practical difference between all these 4 access modifiers in java software development language. I have created 3 classes as bellow. Two(Accessmodif, Access) of them are in same package Test_Package1 and one class (Accessing) is in other package named Test_Package2 of same project. Class Accessing is child class of Accessmodif class.

package Test_Package1;

public class Accessmodif {

 public static int i=10;
 private static String str="Hello";
 protected static double d=30.235;
 static char c='g';
 
 public static void main(String[] args) {
  Testing_pub();
  Testing_pri();
  Testing_pro();
  Testing_Nomod();
 }
 
 //Method with public access modifier. Can be accessible by any class.
 public static void Testing_pub(){
  System.out.println("Testing_pub() Executed");
  System.out.println("Value Of i Is "+i);
  System.out.println("Value Of str Is "+str);
  System.out.println("Value Of d Is "+d);
  System.out.println("Value Of c Is "+c);
 }
 
 //Method with private access modifier. Can be accessible from same class only.
 private static void Testing_pri(){
  System.out.println("Testing_pri() Executed"); 
 }
 
 //Method with protected access modifier. Can be accessible from any class of same package or sub class of this class.
 protected static void Testing_pro(){
  System.out.println("Testing_pro() Executed");
 }
 
 //Method with no access modifier. Can be accessible by all classes of same package Only.
 static void Testing_Nomod(){
  System.out.println("Testing_Nomod() Executed");
 }
}

package Test_Package1;

public class Access {

 public static void main(String[] args) {
  Accessmodif.Testing_pub(); //Can access public methods outside the class or package.
  //Accessmodif.Testing_pri(); - Can not access private methods outside the class
  Accessmodif.Testing_pro();  //Can access protected methods inside same package class.
  Accessmodif.Testing_Nomod(); //Can access no modifier methods inside same package class.  
 
  System.out.println();
  System.out.println("Value Of i Is "+Accessmodif.i); //Can access public variables outside the class or package.
  //System.out.println("Value Of str Is "+Accessmodif.str); - Can not access private variables outside the class
  System.out.println("Value Of d Is "+Accessmodif.d); //Can access protected variables inside same package class.
  System.out.println("Value Of c Is "+Accessmodif.c); //Can access no modifier variables inside same package class.
 }
}

package Test_Package2;

import Test_Package1.Accessmodif;

public class Accessing extends Accessmodif{

 public static void main(String[] args) {
  Testing_pub(); //Can access public methods outside the package.
  //Testing_pri(); - Can not access private methods outside the class. 
  Testing_pro(); //Can access protected methods inside child class.
  //Testing_Nomod(); - Can not access no access modifier methods outside the package.
  
  System.out.println();
  System.out.println("Value Of i Is "+i); //Can access public variables outside the package.
  //System.out.println("Value Of str Is "+str); - Can not access private variables outside the class.
  System.out.println("Value Of d Is "+d); //Can access protected variables inside child class.
  //System.out.println("Value Of c Is "+c); - Can not access no access modifier variables outside the package.
 }
}

As per above example, Now its clear that based on access modifiers, we can access variables, methods, etc.. in any other package or class. If you will un comment the commented methods or variables from above given classes, It will show you error related to access level.

14 comments:

  1. where protected cant access

    ReplyDelete
    Replies
    1. protected can not be accessible outside the package.

      Delete
  2. I understood it already. Thank you for the clear explanations. :)

    ReplyDelete
  3. It's still confusing where we cant access the protect variable or method because as in the explanation above it can accessible only within the package but in the Package 2 variable "d" accessed and printed??? Am I making any sense ?

    ReplyDelete
    Replies
    1. Is it sub class of class? Then it can be accessible outside package too. Expatiation also says "or sub classes of that class."

      Delete
  4. Protected cannot be accessed in different package, if the class in different package is child class then it can be accessed

    ReplyDelete

  5. Wonderful blog.. Thanks for sharing informative blog

    Software Tutorials

    ReplyDelete
  6. Protected can be accessed from subclass be it in same or different package. to access in different package parent class must be declared as public.

    ReplyDelete
  7. Just want to clear, methods and variable with no access modifier cannot be accessed by subclass... is it right ?

    ReplyDelete
  8. Yes , Methods and variables with no Access modifiers can only be access by same class and same package not even by subclass or other packages .More over if no access modifier is there then by default it will be considered as Private key word Partially.Basic difference is, in Private keyword it cannot be even access by same Package.

    ReplyDelete