What is Access modifiers in Java?

, , No Comments

 Java is an object-oriented programming language, we have to follow the encapsulation wherein we hide the unwanted details.

Java provides entities called “Access Modifiers or access specifiers” that help us to restrict the scope or visibility of a package, class, constructor, methods, variables, or other data members. These access modifiers are also called “Visibility Specifiers”.

Access Modifiers In Java
To ensure encapsulation and reusability, these access specifiers/modifiers are an integral part of object-oriented programming.


Modifiers in Java are of two types:

#1) Access Modifiers

Access modifiers in Java allow us to set the scope or accessibility or visibility of a data member be it a field, constructor, class, or method.

#2) Non-access Modifiers

Java also provides non-access specifiers that are used with classes, variables, methods, constructors, etc. The non-access specifiers/modifiers define the behavior of the entities to the JVM.

Some of the non-access specifiers/modifiers in Java are:

  • static
  • final
  • abstract
  • transient
  • volatile
  • synchronized
  • native
    

Types Of Access Modifiers In Java

Java provides four types of access specifiers that we can use with classes and other entities.

These are:

#1) Default: Whenever a specific access level is not specified, then it is assumed to be ‘default’. The scope of the default level is within the package.

#2) Public: This is the most common access level and whenever the public access specifier is used with an entity, that particular entity is accessible throughout from within or outside the class, within or outside the package, etc.

#3) Protected: The protected access level has a scope that is within the package. A protected entity is also accessible outside the package through inherited class or child class.

#4) Private: When an entity is private, then this entity cannot be accessed outside the class. A private entity can only be accessible from within the class




0 टिप्पणियाँ:

Post a Comment