Access Modifiers in Java

Video Tutorial
FREE
Classes & Objects I thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Access Modifiers in Java provides a restriction on the scope of the class, instance variables, methods, and constructors.

There are four Access modifiers in Java- Default, Private, Protected, and Public. In Java, access modifiers control visibility. private restricts access, like keeping personal salary details within the family. public allows unrestricted access, akin to everyone knowing your name. protected is similar to public but with some limitations. default serves as a baseline, accessible within its package. These modifiers manage visibility, for instance, variables, constructors, and methods.

Types of Access Modifiers in Java

1. Default Access Modifier

In Java, when class members and methods are not explicitly assigned any access modifier, they default to having package-private access. This means they can only be accessed within the same package, hence often referred to as package-private.

Real-life analogy: Imagine setting your Facebook privacy status to "visible to your friends only". Similarly, package-private access restricts access to members and methods to within the same package, akin to only allowing known friends to view your status.

Understanding package-private access with a Java example:

Access within the same package: Members and methods with default access can be accessed freely within the same package. Access from another package: Attempting to access default members from another package results in an error, as default access doesn't permit this.

Example 1

In the example below is a package First containing two classes. We are trying to access the first-class default method in the second class. Let's see what will happen.

First Package

Output:

The program will run successfully because default members and the methods can be accessed in the same package.

Example

In the below example, we are accessing the default method of the First package class in another package, i.e. Second.

Second Package

Output:

We got an error because the default access modifiers in Java don't allow us to access the members and methods in another package. We are accessing the default method in another package in the above program.

2. Private Access Modifier

The private modifier in programming limits access to certain data members and methods within a class. It's like setting your Facebook status to "only me" - only you can see it, and similarly, only the class itself can access private members. Even other classes in the same package can't access them.

Example

In the below class, we have two private instance members and a constructor, as well as a method of private type.

Output:

3. Protected Access Modifier

Protected access in Java is useful for allowing access within the same package as well as by subclasses, even if they're in a different package. To access protected members from another package, you need to extend the class that contains those members. This means creating a child class in the other package. Simply creating an object of the class won't grant access to its protected members; you need to inherit them through a subclass.

There are two packages, First and Second, The First package contains one public class and a protected prefixed method, and we are trying to access this method in another package in two ways.

  • Creating an instance of the Scaler class (declared in First package)
  • Inheriting the Scaler class in the InterviewBit class of the Second package.

Let's see what will happen.

First package:

Second package:

The line ob.print() will cause an error because we cannot access the protected method outside its package, but we can access it by inheriting it in some other class of different packages, that's why ob1.print() will not cause any error.

These are the four Access Modifiers in Java.

4. Public Access Modifier

The public access modifier in Java means there are no restrictions on accessing the methods, classes, or instance members of a particular class. It allows access from any package and any class. A real-life example is setting a Facebook status to "public," allowing anyone on Facebook, whether a friend or not, to see it. This flexibility makes the public modifier useful for wide accessibility.

Example

Consider two packages named First and Second with two public classes.

The Scaler class of the First package contains one instance member and one method, i.e. print(). Both are of public type. Let's try to access them in another class of different packages, i.e. in the Second package.

First package:

Second package:

Output:

We got the correct output, because we can access the public modifier's methods or instance variables in any class of the same or different package.

Understanding Java Access Modifiers

Access ModifierSame classSame package subclassSame package non-subclassDifference Package subclassDifferent package non-subclass
DefaultYesYesYesNoNo
PrivateYesNoNoNoNo
ProtectedYesYesYesYesNo
PublicYesYesYesYesYes

The above table depicts the restriction levels of different kinds of Access Modifiers. With the help of this table, we can distinguish between all the modifiers available in Java.

Algorithm to Use Access Modifier in Java

The following steps can be followed to choose an appropriate access modifier based on the desired level of visibility.

  • Determine the scope of the variable or method that needs to be accessed.
  • Use the public access modifier if the variable or method needs to be accessible from anywhere in the program, including outside classes.
  • Use the protected access modifier if the variable or method needs to be accessible within the same package or subclass but not outside the package or subclass.
  • Use the default (package-private) access modifier if the variable or method needs to be accessible within the same package only.
  • Use the private access modifier if the variable or method needs to be accessible only within the same class.

Conclusion

  • Java has four access modifiers: Public, Private, Default, and Protected.
  • Private offers the most restricted access, while public offers the least.
  • Private members are accessible only within their class.
  • Public members are accessible from anywhere.
  • Default, or package-private, access allows access within the same package.
  • Protected access allows access within the same package or by subclasses in different packages.
  • Private doesn't permit method overriding, but other modifiers do, both within the same package and across packages.

FAQs

Q: Why are Access Specifiers important?

A: Access specifiers in java are used to provide encapsulation and different types of restrictions according so that we can avoide sensitive information from direct access.

Q: How many Access Modifiers are there in Java?

A: Four Access modifiers are present in Java, public, private, protected, and default.

Q: Which Modifiers are not used for the class?

A: The private and protected modifiers cannot be used for the class because if they are used for the class, due to their restricted scope, JVM will not be able to execute the class because of JVM's external calling system.