As we all know, Java is one of the top programming languages in the world. Billions of devices are relying on it for the last two decades. Java is a fast, dependable, safe, and multi-platform language. Java runs on any device as long as that device has Java Runtime (JRE), making it multi-platform, fast, and dependable. Access modifier is the property of java, which makes it safe across the multi-platform. Java provides class-level safety (during encapsulation) to the programmer by using the access modifier property. According to the book, Class is the blueprint for building an object in java, which makes it a ‘Building block’ for the program as Java is an Object-Oriented language. An access modifier specifies how any class can access a given class and its fields, constructors, and methods within and different packages. Class, fields, constructors, and methods can have one of four different Java access modifiers.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The following table summarizes how we can apply Java access modifiers to the program:
Modifiers | Class | Packages | Sub-Class | World |
Private | Y | N | N | N |
Public | Y | Y | Y | Y |
Protected | Y | Y | Y | N |
Default | Y | Y | N | N |
We will cover each Java access modifier in the following sections.
When any class, data member, and variable is declared by not writing with an access modifier, then it is set to ‘default’ access modifier. The ‘default’ access modifier means the code inside any class can access the entire program within the same package.
Example #1:
//Java program to show the default modifier. package Test; //Where Class eduCBA is having Default access modifier as no access modifier is specified here class eduCBA { void display () { System.out.println("Hello World!"); } }
Output:
Hello World!
Example #2:
//Java program to show error while using class from different package with default modifier package test2; import test.*; //This class check is having default access modifier class Check { public static void main(String args[]) { //accessing class eduCBA from package test eduCBA obj = new eduCBA(); obj.display(); } }
Output:
Compile-time error.
Syntax ‘protected’ is used by users when they want to use this access modifier.
For Example:
//Java program to show to protected access modifier package test; //Class eduCBA public class eduCBA { protected void display () { System.out.println("Hello World!"); } } //Java program to show to protected modifier in same sub-classes of different packages package test2; import test.*; //Class pro is subclass of eduCBA class pro extends eduCBA { public static void main(String args[]) { pro obj = new pro(); obj.display(); } }
Output:
Hello World!
Users can declare a class, method, constructor, and interface with a ‘public’ access modifier, which can access by any class, method, constructor, and interface within or different packages.
For Example: –
//Java program to show to public access modifier package test; public class eduCBA { public void display () { System.out.println("Hello World!"); } } package test2; import test.*; class pub { public static void main (String args []) { eduCBA obj = new eduCBA (); obj.display (); } }
Output:
Hello World!
When a method or variable is marked as ‘private’ access modifiers, then code inside that same class can only access those methods and variables.
User can not declare any super-class with a ‘private’ access modifier in the program. If the user does so with any class, it makes that class not accessible to any other class in the same package, making class none of the use. However, the user can declare variables and methods within the class with a ‘private’ access modifier, so anyone cannot utilize those variables and methods.
Occasionally people got confused with ‘private’ and ‘protected’ access modifiers, but they both are different.
For Example: –
//Program to show error while using a class from different packages with private modifier. package test; class eduCBA { private void display() { System.out.println("Hello World!"); } } class Check { public static void main (String args[]) { eduCBA obj = new eduCBA(); //make class check to access private method of another class eduCBA. obj.display(); } }
Output:
error: display() has private access in eduCBA obj.display();
Java access modifier gives you an extra edge over your program when you make it public. As we study above, Different types of access modifiers in JAVA and their specification.
So keep in mind every time you use one of them as a class or interface access as they don’t only provide access but also overrides them. While there is always a concern regarding the accessibility of the method in the program. For instance, if an interface is assigned the ‘default’ access modifier in the superclass, it can override access modifiers used in the method’s subclass.
Note: Class includes variables, constructors, fields, and methods, and the interface includes specific fields or methods.The above is the detailed content of Access Modifiers in Java. For more information, please follow other related articles on the PHP Chinese website!