HudaTutorials.com

Java Access Modifiers - Access Modifiers in Java, Specifiers

Last updated on

Access Modifiers in Java

Access Specifiers in Java

Controlling Access to Members of a Class

Java Modifiers

Java Access Modifiers

The access modifiers in Java specifies the accessibility of a field, method, constructor and class. Java access modifiers can change the access level of fields, constructors, methods and class.

Java access level modifiers determine whether other classes can use a particular field or invoke a particular method or invoke a particular constructor.

What is Access Modifier in Java ?

The Java access modifier specifies which classes can access a given class and its fields, constructors and methods. Access level modifiers can be specified for a class, fields, constructors and methods.

How Many Types of Access Modifiers in Java ?

There are four types of Java access modifiers.

What are the four access modifiers in Java ?

  1. Private Access Modifier
  2. Default Access Modifier
  3. Protected Access Modifier
  4. Public Access Modifier

How Many Levels of Access Controls in Java ?

There are two levels of access controls in Java.

  • At the top level ( class level ) : public, or package-private (no explicit modifier).
  • At the member level : public, private, protected, or package-private (no explicit modifier).

At the top level ( class level )

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

At the member level

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Private Access Modifier

The access level of a private modifier is only within the class. It cannot be accessed from outside the class. The private access modifier is specify with Java private keyword.

The private modifier is used to specify access level for constructors, fields and methods only. It is not for class.

Default Access Modifier

The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default. There is no keyword to specify the default modifier.

The default modifier is used to specify access level for class, constructors, fields and methods.

Protected Access Modifier

The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package. The protected access modifier is specify with Java protected keyword.

The protected modifier is used to specify access level for class, constructors, fields and methods.

Public Access Modifier

The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package. The public access modifier is specify with Java public keyword.

The public modifier is used to specify access level for class, constructors, fields and methods.

Java Access Modifiers Table

Type private default protected public
class No Yes No Yes
Variable or Field Yes Yes Yes Yes
Method Yes Yes Yes Yes
Constructor Yes Yes Yes Yes
Interface No Yes No Yes