JAVA
Access Specifier
➢ Access specifier controls the accessibility of a member in the same
class, other class, same package or other package.
➢ It is used to set access level for classes , variables or methods etc.
➢ All the access specifiers are keywords. so they should be written in
lower case letter.
➢ Java provides 4 types of access specifiers
▪ Public
▪ Protected
▪ Default
▪ Private
Public Access Specifiers
➢ When a class member is declared as public , it means this member can
be accessed anywhere i.e in any class of any package.
➢ Java source code can contain only one public class , whose name
same as file name.
➢ To declare the class and its members as public , use “public” keyword
Example
package mypack1;
public class A {
public int a; //public variable declared
public void call() // public method declared
{
System.out.println("The value of a-:"+a);
}
}
Create another package and access members of A within class B.
SIbasish Sir/TGI/9937641494 Page 1
package mypack2;
import mypack1.A;
public class B {
public static void main(String[] args) {
A obj = new A();
obj.a = 10;
obj.call();
}
}
Protected Access specifier
➢ A member of a class declared with protected access specifier can be
accessed within the same class and its subclass within same package
or in other package.
➢ To declare the member as protected use, “protected” keyword.
Example
package mypack1;
public class A {
protected static int a; //protected variable declared
protected static void call() // protected method declared
{
System.out.println("The value of a :"+a);
}
}
➢ If user wants to access protected members outside the package , you
need to extends the class using “extends” keyword.
➢ If B class is defined in another package and we want to access the
members of A class within B class, then –
package mypack2;
import mypack1.A;
SIbasish Sir/TGI/9937641494 Page 2
public class B extends A{
public static void main(String[] args) {
a = 10;
call();
}
}
Default Access specifier
➢ The default means , we don’t specify any specifier at all.
➢ Then the class , method or variable will be accessible within the same
class and other classes within same package.
➢ It is known as package level access specifier.
Example
package mypack1;
public class A {
int a; //Default variable declared
void call() // Default method declared
{
System.out.println("The value of a :"+a);
}
}
If B is a class within same package , then members of A can be used within
B.
package mypack1;
public class B {
public static void main(String[] args) {
A obj = new A();
obj.a=10;
SIbasish Sir/TGI/9937641494 Page 3
obj.call();
}
}
Private Access Specifier
➢ Members of a class declared with private access specifier , accessed
only within the class , in which they are declared .
➢ These members are not available for their subclass or other package.
➢ To declare the member as private , use “private” keyword.
➢ A class cannot be declared as private.
Example
package mypack1;
public class A {
private int a; //private variable declared
private void call() // private method declared
{
System.out.println("The value of a :"+a);
}
}
package mypack1;
public class B {
public static void main(String[] args) {
A obj = new A();
obj.a=10; //error because a in private
obj.call(); //error because call() in private
}
}
SIbasish Sir/TGI/9937641494 Page 4
SIbasish Sir/TGI/9937641494 Page 5