Modifiers in Java
A modifier is a keyword that we add to those definitions to change their meaning. In other words, It limits the visibility of the classes, fields, constructors or methods in the Java program.
The functionality of the members of a class or a class itself can be protected from other parts of the program by the presence or absence of modifiers. Java language provides a total 12 modifiers. They are public, private, protected, default, final, synchronized, abstract, native, strictfp, transient, and volatile.
Twelve modifiers can be divided into two categories: access modifiers and non-access modifiers. Access modifiers are also called access specifiers in java. In this tutorial, we will learn access modifiers in detail.
Access Specifiers in Java
Access modifiers/specifiers in java define the boundary for accessing the members of a class and a class itself. In other words, access specifiers are those modifiers that are used to restrict the visibility/accessibility of classes, fields, methods, or constructors.
In short, the accessibility/visibility of data depends on the access specifiers. The access specifiers are also known as visibility modifiers. Java provides four explicit access modifiers in object-oriented languages. They are private, default, protected, and public.
Java supports four types of access modifiers.
Private Access Modifier in Java
Now, create another class B and call private field and method of class A.
Role of Private Constructor
Let’s take an example program where we will declare a constructor of class as private and try to create an object of that class. But, we will get a compile-time error.
Now, create another class B and call class A from class B. As you will call the class A, it will give compile time error.
private class Student
private int roll=4;
private class Name
// Inner class
Ans:- Outer class cannot be private but inner class can be private. Instance variable can be private. The outer class is also known as top-level class. The only five modifiers are applicable to a top-level class. They are public, default, final, abstract, and strictfp. By mistake, if you will try to use any other modifiers with top-level class, you will get a compile-time error: “Modifier private not allowed here“.
Default Access Modifier in Java
➲ The default can apply to the instance variable, local variable, constructor, methods, inner class or outer class.
➲ Default members of a class are visible inside of the class and everywhere within classes in the same package or folder only. Therefore, they can be accessed from outside the classes in the same package but can not be accessed outside the package.
➲ Default members can be inherited to the subclass within the same package only. It cannot be inherited from outside the package.
Let’s understand the default access modifier or specifier with a suitable example program.
In this example program, we have created two packages pack1 and pack2. We are accessing class A from outside its package since class A is a default, not public. Therefore, it cannot be accessed from outside the package.
Protected Access Modifier in Java
➲ Protected access specifiers can be applied to the instance variable, local variable, constructor, methods, inner class but not the outer class.
➲ Protected members are accessible inside the class and everywhere within classes in the same package and outside the package but through inheritance only.
➲ Protected members can be inherited to the subclass.
➲ If we make constructor as protected then we can create the subclass of that class within the same package but not outside the package.
Let’s take an example program to understand the concept of default access specifier.
Program source code 4:
In this example, we have created two packages pack1 and pack2. The Student class of pack1 package is public. So, it can be accessed from outside the package. But the name() method of this package is declared as protected. So, it can be accessed from outside the class only through the inheritance.
Let’s see the following source code.
Public Access Modifier in Java
➲ Public access specifier can apply to the instance variable, constructor, inner class, outer class, method but not a local variable.
➲ Public members of a class can be used anywhere.
➲ Public members of a class can be inherited to any subclass.
Let us take an example program related to public access specifier.
Private Protected Access in Java
Visibility of Access Specifier in Java
|Access location||Public||Protected||Default||Private protected||Private|
|Subclass in same package|
|Other classes in same package|
|Subclasses in other package|
|Non-subclasses in other packages|
Key of Access modifiers
Private > Default > Protected > Public
More restrictive —————-> Less restrictive.
Applicable modifiers with Classes, Methods, Variables, Interfaces, etc.
Outer Class (Top-level Class)
2. Final and abstract both cannot be applied simultaneously with a class. It is an illegal combination.
3. If a class is declared as public, private or protected cannot be applied simultaneously with public class. It is also an illegal combination.
2. The non-applicable modifiers for inner class are synchronized, native, transient, and volatile.
3. The modifiers which are not applicable for inner classes but not applicable for outer classes are private, protected, and static.
2. If a method is declared as public, we cannot declare simultaneously private or protected with a public method.
3. If a method is defined as abstract, we cannot apply final, static, synchronized, native, private, or strictfp.
2. The non-access modifiers such as abstract, synchronized, native, and strictfp are not applicable for variables.
3. The only applicable modifier with local variable is final. If a variable is declared as final, we cannot declare as volatile.
2. Non-access modifiers cannot be applied with constructors. By mistake, if you apply any other modifiers with constructor except these four access modifiers, you will get a compile-time error.
2. The non-access modifier that is applicable for classes but not applicable for an interface, is final.
2. The non-access modifiers such as final, synchronized, native, transient, and volatile are not applicable for inner interface.
2. The modifiers which are applicable for classes but not applicable for enum are final and abstract.
Hope that this tutorial has covered almost all important points related to access modifiers/specifiers in java with example programs. I hope that you will have understood access specifiers and their application. In the next tutorial, we will discuss about non-access modifiers in java.Thanks for reading!
⏪ PrevNext ⏩