Vous êtes sur la page 1sur 6

Access Modifiers In Java - Java Tutorials For

Selenium WebDriver
Access modifiers are the keywords in java by
which we can set the level of access for class,
methods, variables and constructors. There are
4 different access modifiers available in java.
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 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. 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.

Vous aimerez peut-être aussi