Access Specifier

In this section, we will discuss more about the Access specifier in Java. It restrict the scope of variables and methods.

Objective:

  • What is Access Specifier in Java?
  • Types of Access Specifier
  • Why It is needed?
  • Examples

Access specifier:

From the name you can guess what Access Sin java, we will not mention any access specifier, and it will take as Default access. The member with default access only can be accessed within the package only. It has less scope of visibility than protected and public.

Access Specifier is nothing but the feature which is used to restrict the scope or specifies the accessibility of the field, class, method etc. That means we can change our scope of the access by applying the access specifiers to the fields, method, class etc.

There are 4 types of Access specifiers in java .

  • public
  • private
  • protected
  • Default
Access Specifier

Let’s understand each access specifiers in details.

Public:

Public Access specifier allows the field, class, method to be accessed from anywhere in the program. It has the highest visibility in the program. If you want to access the field, class, method etc. anywhere in the application then you need to use public access specifier to the field, class, method.

To specify the public access specifier we need to use the keyword public.

Syntax:

public class
public int id;
public void show(){
--------------------------------
--------------------------------
--------------------------------
}

Example:

//Employee name 
package emp;  
public class name
{  
  public void showName(){
  System.out.println("Employee Name");}  
}  

//Employee Details  
package empDetails;  
import emp.*;  
  
class details{  
  public static void main(String args[]){  
     name n = new name();  
     n. showName();  
  }  
}  

Output:

Employee Name

Explanation:

From the above example you can notice that we have 2 package with name emp and emp details. In emp package we have a class with name emp which is declared as public.

When we are trying to access the same class in different package named empDetails, it is showing the output as expected without any error. So, a class is accessible to another package if it is declared as public. It is widely accessed and has highest visibility of the program.

Private:

This access specifier has the least visibility in the program. This is only accessible within the class. The method or variables which are declared as private , cannot inherited to the subclass. So top class or interfaces cannot be declared as private in java.

To specify the private, we need to use the keyword private. Hence Private member can not be accessed outside the class.

Syntax:

private class
private int id;
private void show(){
-----------------------------
-----------------------------
-----------------------------
}

Example:

class name
{  
private name = "Employee Name" 
private void showName(){
System.out.println("Print Employee Name");
}  
}  
 
class details{  
  public static void main(String args[]){ 	 
   name n = new name();  
   System.out.println("n.name"); // Compile Time Error
   n. showName();  // Compile Time Error
  }  
}  

Output:

Compile Time Error

Explanation:

From the above example you can notice that we have declared the name variable as private and showDetails method as private. As a result, when we are trying to access the name variable by the object of emp class it is throwing compilation error are private member can not be access outside the enclosed class. So we can access only within the emp class. Like name variable, showDetail() method also can be accessed within the emp class only. As we are trying to access outside of the enclosed emp class, it is throwing the compilation error.

Note: You can not create the instance of the constructor if you will declare the constructor as private.Private access specifier can be used with the nested class and not with the top level classes

Example:

class name
{  
  private name(){
  	System.out.println("Constructor");
  }  
}  

class details{  
  public static void main(String args[]){ 	 
   	name n = new name();  // Compile Time Error
  }  
}  

Output:

Compile Time Error

Explanation:

In the above example, the default constructor is declared as private. As the private rule, private method cannot be used outside the enclosed class. So, as this is constructor we can not use this outside of the class to instantiate the object. When we are trying to instantiate the object of class name then this is throwing compile time error.

Protected:

The protected field, class, method can be accessed outside the package only through the inheritance and directly inside the package. So it has the one level high visibility to the private access specifier.

The main difference between private and protected is private is only visible / accessible within the enclosed class. Whereas protected is visible / accessible to inside the package directly and outside the package through inheritance.

Syntax:

protected class
protected int id;
protected void show(){
-----------------------------
-----------------------------
-----------------------------
}

Example:

//Employee name 
package emp;  
public class name
{  
protected void showName(){
System.out.println("Employee Name");}  
}  

//Employee Details  
package empDetails;  
import emp.*;  
  
class details extends name{  
  public static void main(String args[]){  
   details d = new details ();  
   d. showName();  
  }  
}  

Output:

Employee Name

Explanation:

In the above example, you can notice that we have inherit the name details class from the name class. That’s why we are able to access the protected method outside the package.

Note: if we need to use the package member in another package we need to import that package before declaring any member of that package. Otherwise it will throw compile time error.
Note: As we know that to access the protected member we need to use inheritance concept, we cannot declare a class or interface as protected in java.

Default Access:

This is the default access of the field, class, method if we are not mentioning any specifier. In this concept, java differs from the C++. In C++, default access is private. If we will not define any specifier in C++, then it will take that member as private and has least visibility in the program.

But in java, we will not mention any access specifier, it will take as Default. We can access the member with default access within the package only. It has less scope of visibility than protected and public.

Syntax:

class
int id;
void show(){
-------------------------------
-------------------------------
-------------------------------
}

Example:

//Employee name 
package emp;  
class name
{  
void showName(){
System.out.println("Employee Name");}  
}  

//Employee Details  
package empDetails;  
import emp.*;  
  
class details{  
  public static void main(String args[]){  
   name n = new name ();  // Compile Time error
   n. showName();  // Compile Time error
  }  
}

Output:

Compile Time error

Explanation:

In the above example, as we are trying to access default access member outside the package, it is throwing the Compile Time error.

Let’s elaborate the access scope of all access specifiers in a tabular format.

Access SpecifierWithin the classWithin the packageOutside the package with inheritanceOutside the Package
Public YesYes Yes Yes 
PrivateYes No No No 
ProtectedYes Yes Yes No 
Default AccessYes Yes No No 

Summary:

  • Access Specifier is used to restrict the scope or specifies the accessibility of the field, class, method etc.
  • Public Access specifier allows the field, class, method to be accessed from anywhere in the program.
  • Private Access specifier is only accessible within the class. The method or variables which are declared as private access specifier, cannot inherited to the subclass.
  • The protected field, class, method can be accessed outside the package only through the inheritance and directly inside the package. So it has the one level high visibility to the private access specifier.

Leave a Reply

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