Access Modifiers in Java

Java access modifiers are used to define the scope and accessibility of data member, method, constructor, classes with respective to other classes and members. In this tutorial we will learn about various access modifiers like private, public and protected and their effect on the scope and accessibility.

Access level modifiers for member field and methods determine whether other classes can use a particular field or call a particular method.

Java supports four member access modifiers
  • private
  • public
  • protected
  • default

Private Access Modifier

Private members are accessible only with in the same class. Private access modifier is the most restrictive of all access level. Private variables, methods and constructors can only be accessed in its own class not even from classes in same package.

  • keyword "private" is used of private access modifier.

  • Class and interfaces cannot be private.

  • Private access modifier helps to encapsulate your variables and methods which you want to hide from rest of the world.

  • Private members can be accessed by other classes, if class provides a public getter method.

Public Access Modifier

Public classes, interfaces, member variables, member methods and constructor is accessible everywhere. This is the least restrictive of all access modifiers.

  • Keyword "public" is used for public access modifiers.

  • Public is also a top level access modifier. It means not only member variables and methods, we can attach public access modifiers with classes and interfaces.

  • Generally, common routines and variables, utility methods that need to be accessed from everywhere are declared public.

  • All public variables and methods of a super class are inherited by its subclasses.

Protected Access Modifier

Protected member variables, methods and constructors can only be accessed from it's own class, classes of same package and subclasses in other packages. Outside of it's own package, the scope of protected members are limited to only subclasses.

  • Keyword "protected" is used for protected access modifiers.

  • Class and interfaces cannot be protected.

  • Methods and fields of interfaces cannot be declared as protected.

  • Unlike public access modifier, protected have limited visibility outside of it's package. Protected variables and methods are used when we want to restrict it's access from all classes outside of my package except sub-classes.

Default Access Modifier

When we don't specify any access modifiers then default access modifier is used. The accessibility of default members are limited to classes in their own package. Any class out side of their own package cannot access default member variables and member methods.

Default access modifier is similar to protected except even subclasses outside of our own package cannot access it.

The following table shows the access to members permitted by each modifier.
Modifier Class Package Subclass World
protected YES YES YES NO
default(no modifier) YES YES NO NO
private YES NO NO NO
How to choose an access level?
  • If your objects stores some critical data which you don't want to share with any other class(not even with subclasses) then make your member variables private.

  • If you want to expose your private member variable to rest of the world then create a public getter function.

  • If you want to restrict access to a member variable but still want to share it with your subclasses, then make it protected.

  • If your object is not dealing with any critical data then make your member variables and methods public Or alternatively you can provide public getters.