We have classes and objects in Java. A package contains these classes and objects. Classes can also contain other classes, methods, variables, and so on. Java is an object-oriented programming language, thus we must adhere to the principle of encapsulation, in which we conceal the unnecessary details. Access Modifiers In Java You can check out the free Java course to learn more about Modifiers.
We can limit the scope or visibility of a package, class, constructor, function, variable, or other data members by using Java’s “Access Modifiers” or “access specifiers” entities. The term “Visibility Specifiers” is another name for these access modifiers.
A certain class method or variable can be made difficult to access or concealed from other classes by utilising the access specifiers.
Which data members (methods or fields) of a class can be accessed by other data members of classes or packages, etc., is also determined by the access specifiers. These access specifiers/modifiers are a crucial component of object-oriented programming, ensuring encapsulation and reusability.
In Java, there are two types of modifiers:
Java’s access modifiers let us control the scope, accessibility, and visibility of any type of data member, including fields, constructors, classes, and methods.
Non-access specifiers are likewise available in Java and are used with classes, variables, methods, constructors, etc. The JVM’s behaviour of the entities is defined by the non-access specifiers and modifiers.
In Java, a few non-access specifiers and modifiers are:
We have covered static, synchronised, and volatile keywords in our earlier tutorials. We will cover the other non-access modifiers in our future tutorials as they are beyond the scope of this tutorial.
Types Of Access Modifiers In Java
Java provides four types of access specifiers that we can use with classes and other entities.
- 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.
- Public: This is the most common access level and whenever the public access specifier is used with an entity, that particular entity is accessible from within or outside the class, within or outside the package, etc.
Static, synchronised, and volatile keywords have all been covered in previous tutorials. The remaining non-access modifiers are outside the purview of this course and will be covered in subsequent tutorials.
Four different sorts of access modifiers are provided by Java, which we can apply with classes and other things.
- Default: When an access level is left blank or unspecified, it is presumed to be “default.” The default level’s scope is contained within the package.
- Public: This is the most prevalent access level, and when it is applied to an object, the public access specifier makes that entity entirely accessible from inside or outside the class, inside or outside the package, etc.
- Protected: The scope of the protected access level is included in the package. Through an inherited class or child class, a protected entity can also be accessed outside of the package.
- Private: A private entity is one that cannot be accessed from outside the class. Only from within the class is it possible to access a private entity.
Default Access Specifiers
In Java, a default access modifier has no particular keyword. When the access modifier is not supplied, the default value is assumed. Classes, methods, and variables are examples of entities that can have default access.
All classes inside the package in which the default class is declared can access this class because it is available within the package but not from outside of it.
Similarly to this, a default method or variable can only be accessed within the package in which it was defined.
Both the class and method display will have default access if we have a class and a method inside of it without any access modifiers. Then it becomes clear that calling the function and directly creating a class object are both possible.
Public Access Modifier
Any class or package in the Java program can access a class, method, or data field that has the ‘public’ prefix. Both inside and outside of the package, one can access the public entity. Public access modifiers often have no restrictions placed on the entity.
Protected Access Specifier
Subclasses of the class where the entity is declared may access entities thanks to the protected access specifier. As long as the class attempting to access a protected entity is a subclass of this class, the entity is accessible regardless of whether the class is in the same package or a separate package.
Keep in mind that a class or an interface cannot be protected, meaning that we cannot apply protected modifiers to them.
Parents and children frequently utilise the protected access modifier.
Private Access Modifier
The accessibility level for the ‘private’ access modifier is the lowest. Privately declared methods and fields cannot be accessed from outside the class. Only the class that includes these private entities as members has access to them.
Keep in mind that the class’s subclasses cannot even see the private entities. Java’s encapsulation is ensured with a private access modifier.
There are a few things to remember while using the Private Access Modifier.
- Classes and interfaces cannot use the private access modifier.
- Private objects (methods and variables) are only accessible within the class in which they are declared.
- An object of a class with a private constructor cannot be created from somewhere else, such as the main method.
Modifiers are a very important concept to learn in Java. You can learn Java for free online if you are interested.