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.

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.

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.

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

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.

  • 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)

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

protected inner class from other class in different package

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

Leave a Reply

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