Access Modifier

An access modifier is used for restricting the accessibility or scope of a class, constructor, data member and method in another class. In Java there are 04 types of access modifier:

  • default
  • private
  • protected
  • public

1. Default access modifier

If we don’t mention or use any access modifier then it is called as default access modifier. The scope of this modifier is limited to the package level only. It means that if we have a class with the default access modifier in a package, only those classes that are in this package can access this class. No other class outside this package can access this class. Similarly, if we have a default method or data member in a class, it would not be visible in the class of another package.

In the below example we are using two classes belong to two different packages. Here in Test class, we are trying to access default method of DisplayTest class. This program will throw compilation error, because the access of default modifier is limited to the same package in which it is declared.

Let us see an example:

	package TestPackage1;

	public class DisplayTest {

		// Here we use default access modifier for displayName()
		String displayName(String name) {
			return name;
		}
		
	}


	package TestPackage2;

	import TestPackage1.DisplayTest;

	public class Test {

		public static void main(String[] args) {

			DisplayTest obj = new DisplayTest();

			// The below method will throw error because we are trying

			// to access the default method in another package

			obj.displayName("HugeLearning");

		}

	}
Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:

The method displayName(String) from the type DisplayTest is not visible

at TestPackage2.Test.main(Test.java:12)

2. Private access modifier

The access of private modifier is limited to class only. Also class and interface can not be declared as private and if a class has private constructor then you can not create the object of that class from outside of the class.

Let us see an example:

	package TestPackage2;

	class Hello {

		int rollNo = 101;
		private String name = "Herry";

	}

	public class Test {

		public static void main(String[] args) {

			Hello obj1 = new Hello();
			System.out.println(obj1.rollNo);
			System.out.println(obj1.name);

		}

	}
Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:

The field Hello.name is not visible at TestPackage2.Test.main(Test.java:12)

3. Protected access modifier

Protected data member and method are only accessible by the classes of the same package and the subclasses present in that package.

Let us see an example:

	package TestPackage1;

	public class DisplayTest {

		// Here we use protected access modifier for displayName()
		protected String displayName(String name) {

			return name;

		}

	}

The below class in the same package can access the method displayName() directly.

package TestPackage1;

	public class Test2 {

		public static void main(String[] args) {

		DisplayTest obj = new DisplayTest();
		System.out.println(obj.displayName("Huge Learning Test2"));

		}

	}

Output:

Huge Learning Test2

 

The following class is in different package but it extends the DisplayTest class so it can access the method name directly: Let us see this example

package TestPackage2;

	import TestPackage1.*;

	class Test extends DisplayTest {

		public static void main(String[] args) {

			Test obj = new Test();
			System.out.println(obj.displayName("Huge Learning Test"));

		}

	}

Output:

Huge Learning Test

 

4. Public access modifier
Public members, methods and classes can be accessed from anywhere. This modifier has the widest scope and is accessible from anywhere. Let us see an example:

	package TestPackage1;


	public class DisplayTest {

		public String displayName(String name) {

		return name;

		}

	}


	package TestPackage2;

	import TestPackage1.*;

	class Test extends DisplayTest {

		public static void main(String[] args) {

		DisplayTest obj = new DisplayTest();
		System.out.println(obj.displayName("Huge Learning Test"));

		}

	}
Output:

Huge Learning Test