Access Specifier

Java Access Specifiers (also known as Visibility Specifiers) regulate access to classes, and fields and methods in Java. This Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class.

Access Specifiers can used to restrict access. Access Specifiers are integral part of object-oriented programming.

Java has four Access Specifiers which are listed below.

  • Public
  • Private
  • Protected
  • Default

public Access Specifier

Public Specifiers achieves the highest level of accessibility. In java Classes, methods, and fields declared as public can be accessed from any class in the Java program, whether these type classes are in the same package or in another package.

Example:


public class Demo //public class 
{ 
 public x, y, size;   // public instance variables   
} 
								

Example:

	
package p1;
public class A
{
public void display()
{
System.out.println("DataFlair Example");
}
}


package p2;
import p1.*;
class B
{ 
	public static void main(String args[]) 
	{ 
		A obj = new A; 
		obj.display(); 
	}
}	



// Output 
DataFlair example				

private Access Specifier

Private Specifiers achieves the lowest level of accessibility. Private methods and fields that can only be accessed within the same classes to which the methods and fields belonging.

Private methods and fields are not visible within the subclasses and these are not inherited by subclasses. So, private access Specifier is opposite to the public access Specifier. Using Private Specifier we can be achieved encapsulation and hide data from outside world.

Example 1:


public class Demo  // public class
{      
	private double x, y;   // private (encapsulated) type instance variables
}   								

Example: Java program to illustrate error while sing class from different package with private modifier

	
package p1;
class A
{
	private void display()
	{
		System.out.println("Indian Times");
	}
}


package p2;
import p1.*;
// class is having default access modifier
class ExampleNew
{
	public static void main(String args[])
	{
		//accessing class from package p1
		Example obj = new Example();
		obj.display();
	}
}							

Output :

	
error: display() has private access in A
obj.display();						

protected Access Specifier

The Methods and fields declared as protected can only be accessed by the subclasses in other packages or any classes within the package of the protected member type class. The protected access Specifier can’t be applied to the class and interfaces. Example: :


public class Demo //public class  
{ 
	protected double x, y; //protected instance variables 
}								

Example: :


package p1;
//Class A
public class A
{
	protected void display()
	{
		System.out.println("DataFlair example");
	}
}

package p2;
import p1.*; //importing all classes in package p1
//Class B is subclass of A

class B extends A
{
	public static void main(String args[])
	{
		B obj = new B();
		obj.display();
	}
}
								

Output :


Output
DataFlair example								

In this program, we have declared two classes P1 and P2, where class A is in P1 and is made public for it to be accessed in P2.

Default or (No Access Specifier)

When we don't set access Specifier for element then it will follow the default accessibility level. There is no default Specifier keyword. Classes, variables, and methods can be default accessed.

Using default Specifier we can be access class, method, and field which belongs to same package, but not from outside this package. Example :


class Demo   
{  
 int i;  //Default Access Specifier.
}							


package p1;
//Class Example is having Default access modifier
class Example
{
	void display()
	{
		System.out.println("Hello java!");
	}
}

package p2;
import p1.*;
//This class is having default access modifier
class ExampleNew

{
	public static void main(String args[])
	{
		//accessing class Example from package p1
		Example obj = new Example();
		obj.display();
	}
}								

Output


Compile time error								

The following table will give us more understanding of Access Specifier private-protected-public-default-in-java

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group