Table of Contents
Access Modifiers in Java
Access Modifiers define the scope of entities in Java. The entities include classes, constructors, variables, methods, and/or fields. Java has the following four access modifiers:
- Default – No Keyword required
- Private
- Protected
- Public
Default | Private | Protected | Public | |
Same class | Yes | Yes | Yes | Yes |
Same package subclass | Yes | No | Yes | Yes |
Same package non-sub class | Yes | No | Yes | Yes |
Different package subclass | No | No | Yes | Yes |
Different package non-subclass | No | No | No | Yes |
Default
As the name suggests, when no access modifier is specified, Java assumes the default access modifier for the entity, the scope of which is given in the table above. The default modifier makes classes, methods, and variables only visible inside the same package.
Sample Code
In the code below, we will be creating two different packages and place two classes inside. We will then try to access a class of one package from another class of another package. The ‘default’ access modifier will not allow this.
package p1; class Demo1 { void display () { System.out.println (“Hello World”); } } package p2; import p1.*; class Demo2 { public static void main (String args []) { Demo1 obj = new Demo1(); obj.display (); } }
OUTPUT
//As expected, this will result in a compile-time error as the default modifier does not allow access outside its package.
Private
To use this modifier, the keyword private needs to be used in the declaration of the entity. Private gives restricted access such that the scope is only within the class. An entity declared private cannot have any interactions with code outside the scope of its class. Any other class of the same package will not be able to access private members. Another feature of private access modifiers is that top-level classes or interfaces can not be declared private. This is because private truly means only accessible within the class, and protected means only being visible to the current class and its subclasses. Therefore, these modifiers do not apply to nested classes.
Sample
In the sample code, we define two classes within the same package. A method in one of the classes will be defined as private, and this method will be accessed from another method of the other class.
package p1; class A { private void display () { System.out.println (“Hello World”); } } class B { public static void main (String args []) { A obj = new A(); obj.display (); } }
OUTPUT
error: display () has private access in A
obj.display ();
Protected
The protected modifier can be used with the keyword ‘protected’. The methods or data members declared protected can only be accessed by entities within the same class and subclasses of that class that can be in different packages as compared to the package of the superclass.
Sample
In this example, two packages will be defined. Class A of package 1 will be declared public. The method display in A will be protected, and class B will be inherited from class A. The protected method will then be accessed by creating an object of class B.
package p1; public class A { protected void display () { System.out.println (“Hello World”); } } package p2 import p1.*; class B extends A { public static void main (String args []) { B obj = new B(); obj.display (); } }
OUTPUT
Hello World
Public
Public is specified using the ‘public’ keyword, and members defined public can be accessed from any package by any other entity.
Sample
package p1; public class A { public void display () { System.out.println(“Hello World”); } } package p2; import p1.*; class B { public static void main (String args []) { A obj = new A ; obj.display (); } }
OUTPUT
Hello World
0 Comments