Details about access modifiers in Java - Selenium Tutorials Beginners

Access modifiers are used in java to set the level of access for methods, class, variables and constructors. There are four different types of access modifiers in java and each has different methods to set access.

Public Access Modifier

This is used to access public methods or variables from all classes of the same package or other packages.

Private Access Modifier

This is to access private methods and variables from same classes. Methods and variables from other classes or subclasses even if they are in the same package cannot be accessed.

Protected Access Modifier

This can be used to access protected methods in the subclass of the same class or from the classes of the same package.

No Access Modifier

This can be used to access methods of all classes in the same package if methods have no access modifier.

The three classes created below will demonstrate the practical difference between these four access modifiers in java. Among the three, “accessmodif”, access are in the same package and “Accessing” is in the other package.

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.
}
}

These examples clearly indicate that methods, variables etc can be accessed in the same package or in different packages based on the access modifiers.

Leave a Reply

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