Java Access Modifiers:

  • A Java access modifiers specifies which classes can get right of entry to a given class and its fields, constructors and methods.
  • Access modifiers can be detailed one after the other for a class, its constructors, fields and methods.
  • Java get entry to modifiers also are now and again mentioned in daily speech as Java access specifiers, however the proper name is Java access modifiers.

Four Types of Access Modifiers:

    1. Default (Visible to the package)
    2. Private (Visible to the class only)
    3. Public (Visible to the world)
    4. Protected (Visible to the package and all sub classes)



Default Access Modifier:

  • Default Java access modifiers manner we do no longer explicitly declare an access modifier for a classsubjectmethodetc.
  • A variable or method declared without any access manipulate modifier is available to some other class in the equal package.
  • The default modifier is available only within package.


Step 1: First we create a class DefaultClass under a package pack in which we declare default method:

Step 2: Second we create a class DefaultAccess under a package mypack and imports the above package pack:

Explanation of Example:

In the above example, the scope of class DefaultClass and its method show() is default so it cannot be accessed from outside the package. As a result it shows compile time error.


Private Access Modifier:

  • Methods, Variables and Constructors which might be declared private can best be accessed within the declared class itself.
  • Private get admission to modifier is the maximum restrictive access level.
  • Class and interfaces cannot be private.
  • Variables which can be declared private can be accessed outdoor the class, if public methods are present in the class.
  • using the private modifier is the primary way that an object encapsulates itself and cover information from the outside global.


Step 1: First we create class PrivateClass in which we declare one private data member and one private method:

Step 2: Second we create a class PrivateAccess in which we call the PrivateClass class data member and method:

Explanation of Example:

In PrivateAccess class when we are trying to call the private data member and method of a PrivateClass class it gives us compile time error because private data members and methods have a access level to PrivateClass class only.


Public Access Modifier:

  • a classmethod, constructor, interface and many others declared public may be accessed from another class.
  • consequently fields, methods, blocks declared internal a public class can be accessed from any class.
  • if you need to access public class from specific package, then you need to import the public class.


Step 1: First we create a class PublicClass in which we declare the public method show():

Step 2: Second we create a class PublicAccess in which we call the method of PublicClass class:



Protected Access Modifier: 

  • The protected Java access modifier is available within package and outside the package but through inheritance only.
  • The protected access modifier may be applied at the information member, method and constructor. it could’t be applied on the class.


Step 1: First we create a class ProtectedClass under a package pack1 in which we declare a protected method show():


Step 2: Second we create a class ProtectedAccess under a package mypack1 and import a package pack1: