Access Modifiers in JAVA

Ravi Chandola
Javarevisited
Published in
3 min readDec 2, 2022
Access and scope of java program

Access modifiers are used to specify the scope of the class and its member. In layman's terms, we can say that access means a way of reaching the place. Here we are talking about our code i.e, a java program where we define the scope of the particular code with respect to its modifiers.

PRIVATE SCOPE

  1. When we are using the private modifier with the member of the class then the scope for those members will be private.
  2. Private members must be accessed from the same class only, not even the subclasses which means we cannot inherit the private class members.
  3. We cannot use theprivate’ modifier for the class.

Now let's see what happens when we want to subclass the private member of the class.

class HelloWorld {
private int a = 60;
}
class ByeWorld extends HelloWorld{
public static void main(String[] args) {
ByeWorld hello = new ByeWorld();
System.out.println(hello.a);
}
}
javac /tmp/gOVuc9VvLe/ByeWorld.java
/tmp/gOVuc9VvLe/ByeWorld.java:11: error: a has private access in HelloWorld
System.out.println(hello.a);
^
1 error

Here we can clearly see that JVM throws an error as “a has private access in HelloWorld” which clearly states point number 2 above.

DEFAULT SCOPE

The default scope is actually scope when we are not using any modifier such as public, private, or protected with the member of the class.

  1. It is also known as packaged scope as the member of this scope can be accessed from the same class, subclass, and non-subclass but available in the same package.
  2. The default members of the class cannot be accessed from outside the package.
  3. Unlike private scope, the default scope is both class and its member.
default scope diagram

So from above, we can say that the default member of class A of package com.demo.medium cannot be accessed by class D, class E or class F of com.demo.javarevisited.

PROTECTED SCOPE

In protected scope, we are using a protected modifier with the member of the class. The member of a protected class can be accessed from the same subclass and non subclass available in the same package.

Note: Protected members can be accessed from the subclasses of different packages

the protected modifier can only be used for the members of the class only.

PUBLIC SCOPE

We say the scope is public when we are using a public modifier with the member of the class. Public members have no restrictions as they can access anywhere in the entire project.

The public modifier can be used for both class and its members.

I hope you understand the topic, let me know if you have any confusion so that I can explain it more deeply with some more interactive examples. For more such miscellaneous topics, do follow and if you like the content don’t hesitate to clap. Stay tuned!

--

--

Ravi Chandola
Javarevisited

As a technology enthusiast, I have a passion for learning and sharing my knowledge with others. https://www.linkedin.com/in/ravi-chandola-304522133