There are four access specifiers in Java viz. private, protected, default and public. These access specifiers can be applied to classes, methods and variables. Having said that, there are some situations where using a specific access specifier is prohibited by Java compiler. For example, private can not be used with abstract keyword because abstract means that the implementation shall be provided by someone else and by making it private we are reducing visibility and hence preventing implementation.

The meaning of each of these four access modifier/specifier is shown below:

public:

public means available to all. Any class, method or variable marked as final can be accessed from any piece of code without any restriction. If there is a public member variable defined in a class and its object is created in another method outside the class. The public member variable can be accessed using the object and dot (.) operator.

default:

This is the most used access specifier in Java programs. When no access specifier is given to a class, method or variable then the default access specifier is used. The meaning of this specifier is that the entity shall be visible in the same package in which it is defined. The difference between default and public is that while public means accessible to all, default means accessible only within the same package.

protected:

protected means that only the classes in the current package and subclasses from other packages can see a particular method, variable or inner class. There are some methods in the java.lang.Object class which have declared as protected. A code example which shows the usage of protected access specifier follows:

Foo.java

package com.example;
 
public class Foo{
 
	protected int a = 10;
	
	protected void test(){
	}
}

BarExample.java

package com.example;

public class BarExample {
	
	public static void main(String[] args) throws ReflectiveOperationException {
		Foo foo = new Foo();
		System.out.println(foo.a);
	}
}

BarSample.java

package com.sample;

import com.example.Foo;

public class BarSample {

	public static void main(String[] args) throws ReflectiveOperationException {
		Foo be = new Foo();
		System.out.println(be.a); //We shall see compiler error here as the protected member variable is invisible 
		//to non-subclasses outside the package
	}
}

BarWorking.java

package com.sample;

import com.example.Foo;

public class BarWorking extends Foo{
	
	void bar(){
		test();
	}
	
	public static void main(String[] args) throws ReflectiveOperationException {
		BarWorking bw =new BarWorking();
		bw.test();
	}
}

private:

private specifier is mostly used with methods and variables only. The purpose is to hide the internal details of a class from the outside world. Any thing which is private can not be accessed outside the current class. Of course, private members of a class can be accessed by using reflection but still there are ways by which access to private member variables can be completely stopped by using custom SecurityManager.

The basic OOPS principles Abstraction and Encapsulation require the presence of private access modifier. A common example of private member variable is the internal character array used by the String class which is not exposed to other classes. Any changes required in this character array are managed to public methods which return a new String object instead of modifying the character array of “this” object.

Note that protected and private can not be used for top level classes as it make the class invisible and hence makes the class unusable. If we use protected or private with top level class, we shall see following compiler error

Illegal modifier for the class Foo; only public, abstract & final are permitted

This is because of the fact that it doesn’t make any sense to use protected and private specifier with top level classes which are meant to be instantiated or sub-classed. Thus we should be applying protected and private to member variables, methods and inner classes only.

Java Access Modifier tutorial – Default,Private,Public,Protected admin Core Java
There are four access specifiers in Java viz. private, protected, default and public. These access specifiers can be applied to classes, methods and variables. Having said that, there are some situations where using a specific access specifier is prohibited by Java compiler. For example, private can not be used...
There are <strong>four access specifiers</strong> in Java viz. private, protected, default and public. These access specifiers can be applied to classes, methods and variables. Having said that, there are some situations where using a specific access specifier is prohibited by Java compiler. For example, private can not be used with abstract keyword because abstract means that the implementation shall be provided by someone else and by making it private we are reducing visibility and hence preventing implementation. The meaning of each of these four access modifier/specifier is shown below: <h3>public:</h3> public means <strong>available to all</strong>. Any class, method or variable marked as final can be accessed from any piece of code without any restriction. If there is a public member variable defined in a class and its object is created in another method outside the class. The public member variable can be accessed using the object and dot (.) operator. <h3>default:</h3> This is the <strong>most used access specifier in Java</strong> programs. When no access specifier is given to a class, method or variable then the default access specifier is used. The meaning of this specifier is that the entity shall be visible in the same package in which it is defined. The difference between default and public is that while public means accessible to all, default means accessible only within the same package. <h3>protected:</h3> protected means that only the classes in the <strong>current package and subclasses from other packages</strong> can see a particular method, variable or inner class. There are some methods in the java.lang.Object class which have <a href="http://www.javaexperience.com/java-cloning-tutorial/" title="Java Cloning Tutorial, Shallow Copy and Deep Copy">declared as protected</a>. A code example which shows the usage of protected access specifier follows: <strong>Foo.java</strong> 1 <strong>BarExample.java</strong> 1 <strong>BarSample.java</strong> 1 <strong>BarWorking.java</strong> 1 <h3>private:</h3> private specifier is mostly used with methods and variables only. The purpose is to <strong>hide the internal details</strong> of a class from the outside world. Any thing which is private can not be accessed outside the current class. Of course, private members of a class can be accessed by using reflection but still there are ways by which access to private member variables can be completely stopped by using custom SecurityManager. The basic OOPS principles <strong>Abstraction and Encapsulation</strong> require the presence of private access modifier. A common example of private member variable is the internal character array used by the String class which is not exposed to other classes. Any changes required in this character array are managed to public methods which return a new <a href="http://www.javaexperience.com/why-the-string-class-is-immutable/" title="Why the String class is Immutable">String object</a> instead of modifying the character array of <a href="http://www.javaexperience.com/this-and-super-in-java/" title="this and super keywords">"this" object</a>. Note that protected and private can not be used for top level classes as it make the class invisible and hence makes the class unusable. If we use protected or private with top level class, we shall see following compiler error <code>Illegal modifier for the class Foo; only public, abstract & final are permitted</code> This is because of the fact that it doesn't make any sense to use protected and private specifier with top level classes which are meant to be <a href="http://www.javaexperience.com/difference-between-new-operator-and-class-forname-newinstance/" title="Difference between new operator and Class.forName().newInstance()">instantiated</a> or <a href="http://www.javaexperience.com/multiple-inheritance-in-java/" title="Multiple inheritance in Java">sub-classed</a>. Thus we should be applying protected and private to member variables, methods and inner classes only.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.