Access Modifiers in Java

What are the Access Modifiers in Java?

Access Modifiers in Java control access to classes, methods, and variables. There are four basic access modifiers and they are public, protected, private and no modifier (Default). We will use the word default instead of no modifier for the rest of the tutorial.

Here’s a summary of the access controls. We will get into more details in each of them

Summary Table of the access modifiers in Java

 PublicProtectedDefaultPrivate
Same Classyesyesyesyes
Subclass Different Packageyesyesnono
Subclass Same Packageyesyesyesno
Any class same packageyesyesyesno
Any class different packageyesnonono

Access Modifiers in Java – public

Public access modifier at class level

the public access modifier is the least restrictive modifier. It allows access to a class or member (method or variable) from any other class. Let’s look at an example :
This is our folder structure:
public access modifier in java
We want to demonstrate how public access modifier works at the class level. Since all 3 classes are public we should be freely able to use them anywhere. From Class2 we instantiate Class1 (same package) and Class3 (different package) and it works well.

package com.studytrails.java.core.modifiers.packageA;
public class Class1 {
}

package com.studytrails.java.core.modifiers.packageB;
public class Class3 {
}

package com.studytrails.java.core.modifiers.packageA;
import com.studytrails.java.core.modifiers.packageB.Class3;
public class Class2 {
	public static void main(String[] args) {
		Class1 class1 = new Class1();
		Class3 class3 = new Class3();
	}
}

public access modifier at the member level

We now create variables and methods in Class1 and Class3 and see whether it can be accessed from class 2.

package com.studytrails.java.core.modifiers.packageA;
public class Class1 {
	public int var1;
	public void mehod1() {
	}
}

package com.studytrails.java.core.modifiers.packageB;
public class Class3 {
	public int var1;
	public void mehod3() {
	}
}

package com.studytrails.java.core.modifiers.packageA;
import com.studytrails.java.core.modifiers.packageB.Class3;
public class Class2 {
	public static void main(String[] args) {
		// can instantiate public class from same package
		Class1 class1 = new Class1();
		// can instantiate public class from different package
		Class3 class3 = new Class3();

		// can access public variable from same package
		System.out.println(class1.var1);
		// can access public variable from different package
		System.out.println(class3.var1);
		
		// can access public methods from same package
		class1.mehod1();
		// can access public methods from another package
		class3.mehod3();
	}
}

So far we have covered the least restrictive type of access modifier in java and that is public. Lets go to the other end of the spectrum and look at the most restrictive modifier- private.

Access Modifiers in Java – private

private modifiers allow access only from within the class. If you have a private method or a private variable, that variable is not accessible from outside the class.

Can there be a private class?

You cannot have a top-level class that is private since there is then no way to access it. However, you can have an inner class that is private. We will learn more about inner classes in the subsequent tutorials but for now, let’s just look at an example. We modify Class1 and add two inner classes to it. One of then inner classes would be private.

package com.studytrails.java.core.modifiers.packageA;
public class Class1 {
	public int var1;
	public void mehod1() {
	}
	private class Class4 {
	}
	public class Class5 {
	}
        public static void main(String[] args) {
		Class1 class1 = new Class1();
               // can instantiate Class5 since its public.
		Class5 class5 = class1.new Class5();
               // can instantiate Class4 from the same class although its private.
		Class4 class4 = class1.new Class4();
	}
}

We can access Class5 from any other class, but not class4 since its private

package com.studytrails.java.core.modifiers.packageA;
import com.studytrails.java.core.modifiers.packageA.Class1.Class5;
public class Class2 {
	public static void main(String[] args) {
                // Can instantiate Class5 since its public
		Class5 class5 = class1.new Class5();
                 // This does not compile since Class4 is private
		//Class4 class4 = class1.newClass4();
	}
}

Accessing private methods and variables in Java

private methods and variables can only be accessed from within the class. They cannot be accessed from any other class, not even the subclass. You can also make the constructor of a method private. We will look at constructors later on but for now just know that Singleton design pattern uses the fact that a constructor can be private (which means you cannot instantiate a class from outside that class).

Access Modifiers in java – protected

The next access modifier that we use is protected. Protected class and members allow access to subclasses within the same package or different package. To understand some of the examples below, you will need to know the concept of class extension. We will get into it in details in the next tutorial so you might want to come back to this tutorial once you have understood that. However for the sake of the rest of the tutorial just know that once class can extend another class so that the extending class has access to some of the members of the extended class. Class extension is a way to add more functionality to a class without modifying that class. We use the following class structure for the examples below
protected and default access modifier

What does protected method or variable mean?

protected means that any class in the same package or a subclass in ANY package can access it. We first look at a protected method.

Protected methods in java

Protected Members in java
In the setup above, Class6 has a protected method and variable.

package com.studytrails.java.core.modifiers.packageA;
public class Class6 {
	protected int var6;
	protected void method6() {
	}
}
  • var6 and method6 are visible in class11 (non derived class in same package)
  • var6 and method6 are visible in Class7ExtendsClass6 (derived class in same package)
  • var6 and method6 are visible in class9ExtendsClass6 (derived class in different package)
  • var6 and method6 are NOT visible in class8 (non derived class in different package)

What is a protected class

You cannot define a top-level class to be protected but you can define an inner class to be protected (inner classes are class within a class)

package com.studytrails.java.core.modifiers.packageA;
public class Class6 {
        # inner class is protected
	protected class Class10 {
	}
}

If the inner class is protected, lets see what happens when we try to instantiate from :

protected inner class from other class in same package

package com.studytrails.java.core.modifiers.packageA;
import com.studytrails.java.core.modifiers.packageA.Class6.Class10;
public class Class11 {
	public static void main(String[] args) {
	// to instantiate an inner class we need an instance of the enclosing class.
       Class6 class6 = new Class6();
        // can instantiate a protected inner class from another class in the same package.
       Class10 class10 = class6.new Class10();
	}
}

protected inner class from other class in different package

package com.studytrails.java.core.modifiers.packageB;
import com.studytrails.java.core.modifiers.packageA.Class6;
// This does not compile, gives an error that says the type is not visible
import com.studytrails.java.core.modifiers.packageA.Class6.Class10;
public class Class8 {
	public static void main(String[] args) {
		Class6 class6 = new Class6();
	}
}

so that does not work. what did we learn? – We learned that a protected class is visible from a class in the same package but is not visible from a class in a different package. What if the other class is in a different package by extends the first class

package com.studytrails.java.core.modifiers.packageA;
public class Class6 {
	protected class Class10 {
                // The constructor has to be public otherwise Class9ExtendsClass6 can see the class but not the constructor.
		public Class10() {
		}
	}
}
package com.studytrails.java.core.modifiers.packageB;

import com.studytrails.java.core.modifiers.packageA.Class6;

public class Class9ExtendsClass6 extends Class6 {
	public static void main(String[] args) {
		Class6 class6 = new Class6();
                // Class10 is now visible since we are using it within an exteded class. 
		Class10 class10 = class6.new Class10();
	}
}

Leave a Reply

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