Object Oriented Programming: Unit I: Introduction to OOP and Java

Access Specifiers

with Example Java Programs

Access modifiers control access to data fields, methods, and classes. There are three modifiers used in Java - • public,• private, • default modifier

Access Specifiers

Access modifiers control access to data fields, methods, and classes. There are three modifiers used in Java -

• public

• private

• default modifier

public allows classes, methods and data fields accessible from any class

private allows classes, methods and data fields accessible only from within the own class.

If public or private is not used then by default the classes, methods, data fields are assessable by any class in the same package. This is called package-private or package-access. A package is essentially grouping of classes.

For example:

package Test;

public class class1

{

public int a;

int b;

private int c;

public void fun1() {

}

void fun2() {

}

private void fun3() {

}

}

public class class2

{

void My_method() {

class 1 obj=new class1();

obj.a;//allowed

obj.b;//allowed

obj.c;//error:cannot access

obj.fun1();//allowed

obj.fun2();//allowed

obj.fun3();//error:cannot access

}

}

package another Test

public class class3

{

void My_method()

class1 obj=new class1();

obj.a;//allowed

obj.b;// error:cannot access

obj.c;//error:cannot access

obj.fun1();//allowed

obj.fun2()//error:cannot access

obj.fun3()//error:cannot access

}

}

In above example,

• We have created two packages are created namely - Test and another_Test.

• Inside the package Test there are two classes defined - class1 and class2

• Inside the package another_Test there is only one class defined and i.e. class3.

• There are three data fields - a, b and c. The data field a is declared as public, b is defined as default and c is defined as private.

• The variable a and method fun10) both are accessible from the classes class2 and class3 (even if it is in another package). This is because they are declared as public.

• The variable b and method fun2() both are accessible from class2 because class2 lies in the same package. But they are not accessible from class3 because class3 is defined in another package.

• The variable c and method fun3() both are not accessible from any of the class, because they are declared as private.

Protected mode is another access specifier which is used in inheritance. The protected mode allows accessing the members to all the classes and subclasses in the same package as well as to the subclasses in other package. But the non subclasses in other package can not access the protected members.

The effect of access specifiers for class, subclass or package is enlisted below-

For example, some variable is declared as protected, then the class itself can access it, its subclass can access it, and any class in the same package can also access it. Similarly if the variable is declared as private then that variable is accessible by that class only and its subclass can not access it.

Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : with Example Java Programs - Access Specifiers