C
C
Core Java
Search…
18) Access Modifiers

What are Access Modifiers?

Access modifiers control the accessibility of a class or an interface, including its mem-bers (methods and variables), by other classes and interfaces within the same or separate packages. By using the appropriate access modifiers, you can limit access to your class or interface and their members.

Java has four access levels:

    1.
    public (least restrictive)
    2.
    protected
    3.
    default
    4.
    private (most restrictive)

Real Life Example:

Public: Anyone can see your profile
Protected : Only your friends
Default: Only specific user group
Private : Only me

Access Modifiers Detail

Access modifiers help you set the level of access you want for your Class, variables as well as Methods.
The access modifiers in java specify accessibility (scope) of a data member, method, constructor or class.
There are 4 types of java access modifiers:
    1.
    Private
    2.
    Default
    3.
    Protected
    4.
    public

1) private access modifier

The private access modifier is accessible only within class.
The private access modifier is the most restrictive access modifier. The members of a class defined using the private access modifier are accessible only to themselves. It doesn’t matter whether the class or interface in question is from another package or has extended the class—private members are not accessible outside the class in which they’re defined. private members are accessible only to the classes and interfaces in which they’re defined.
NOTE: For your real projects, it is possible to access private members of a class outside them, using Java reflection.
NOTE: Although the package-private access is as valid as the other access lev- els, in real projects it often appears as the result of inexperienced developers forgetting to specify the access mode of Java components.
Simple example of private access modifier
In this example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is compile time error.
1
package com.gs.ilp.corejava.accessModifier;
2
3
public class A {
4
private int data = 40;
5
6
private void msg() {
7
System.out.println("Hello java");
8
}
9
}
10
11
public class Simple {
12
public static void main(String args[]) {
13
A obj = new A();
14
System.out.println(obj.data);
15
// Compile Time Error
16
obj.msg();
17
// Compile Time Error
18
}
19
}
20
Copied!
Role of Private Constructor
If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:
1
package com.gs.ilp.corejava.accessModifier;
2
3
class A {
4
private A() {
5
}// private constructor
6
7
void msg() {
8
System.out.println("Hello java");
9
}
10
}
11
12
public class Simple {
13
public static void main(String args[]) {
14
A obj = new A();// Compile Time Error
15
}
16
}
Copied!
Note: A class cannot be private or protected except nested class.

2) default access modifier

If you don't use any modifier, it is treated as default by default. The default modifier is accessible only within package.
The members of a class defined without using any explicit access modifier are defined with package accessibility (also called default accessibility). The members with package access are only accessible to classes and interfaces defined in the same package. The default access is also referred to as package-private.
Classes from the same and separate packages
DEFINING A CLASS BOOK WITH DEFAULT ACCESS:

Conclusion:

Default access can be compared to package-private (accessible only within a package), and protected access can be compared to package- private + kids (“kids” refer to derived classes). Kids can access protected methods only by inheritance and not by reference (accessing members by using the dot operator on an object).
Example of default access modifier
In this example, we have created two packages pack and mypack. We are accessing the A class from outside its package, since A class is not public, so it cannot be accessed from outside the package.
1
//save by A.java
2
package pack;
3
class A {
4
void msg() { System.out.println("Hello");}
5
}
6
//save by B.java
7
package mypack;
8
import pack.*;
9
class B{
10
public static void main(String args[]){
11
A obj = new A();//Compile Time Error
12
obj.msg();//Compile Time Error
13
}
14
}
Copied!
In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the package.

3) protected access modifier

The protected access modifier is accessible within package and outside the package but through inheritance only.
Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.
The members of a class defined using the protected access modifier are accessible to
    Classes and interfaces defined in the same package
    All derived classes, even if they’re defined in separate packages
Access of protected members of the class Book in unrelated and derived classes, from the same and separate packages

Conclusion:

A concise but not too simple way of stating the previous rule is this: A derived class can inherit and access protected members of its base class, regardless of the package in which it’s defined. A derived class in a separate package can’t access protected members of its base class using reference variables.
Example of protected access modifier
In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.
1
//save by A.java
2
package pack;
3
public class A{
4
protected void msg(){System.out.println("Hello");}
5
}
6
//save by B.java
7
package mypack;
8
import pack.*;
9
class B extends A{
10
public static void main(String args[]){
11
B obj = new B();
12
obj.msg();
13
}
14
}
Copied!
Output:Hello

4) public access modifier

The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.
This is the least restrictive access modifier. Classes and interfaces defined using the pub- lic access modifier are accessible across all packages, from derived to unrelated classes.
In the preceding example, class Book and its public member instance variable isbn and method printBook are accessible to class House.
NOTE :The term unrelated classes in this chapter refers to classes that don’t share inheritance relation. For instance, classes House and Book are unre- lated, if neither House derives from Book nor Book derives from House.
Example of public access modifier
1
//save by A.java
2
package pack;
3
public class A {
4
public void msg(){System.out.println("Hello");}
5
}
6
//save by B.java
7
package mypack;
8
import pack.*;
9
class B{
10
public static void main(String args[]){
11
A obj = new A();
12
obj.msg();
13
}
14
}
Copied!
Output: Hello

Understanding all java access modifiers

Let's understand the access modifiers by a simple table.
Access Modifier
within class
within package
outside package by subclass only
outside package
Private
Y
N
N
N
Default
Y
Y
N
N
Protected
Y
Y
Y
N
Public
Y
Y
Y
Y

Java access modifiers with method overriding

If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.
1
package com.gs.ilp.corejava.accessModifier;
2
3
class A {
4
protected void msg() {
5
System.out.println("Hello java");
6
}
7
}
8
9
public class Simple extends A {
10
void msg() {
11
System.out.println("Hello java");
12
}
13
14
// compile time Error
15
public static void main(String args[]) {
16
Simple obj = new Simple();
17
obj.msg();
18
}
19
}
Copied!
The default modifier is more restrictive than protected. That is why there is compile time error.
Last modified 2yr ago