×

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields.

If you are aware of C++ language, when we declare any member inside a class without public keyword then it is by default taken as private. But in Java, there are four different types of access modifiers, and if we do not specify access modifier for member or class it is considered as default modifier.

We can use modifiers with the class, but we can use only two access specifier (public or default) with the outer class. Whereas, for inner class we can use any of the four modifiers.

There are four types of access modifiers in Java:

  • Private: Its access level is only within the class. It can’t be accessed from outside that class.
  • Protected: Its access level is within the package and outside the package through child class.
  • Public: Its access level is everywhere because it can be accessed within the class, outside the class, within the package, and outside the package. This is why the main() method has always been defined by the public modifier.
  • Default: Its access level is only within the package because it cannot be accessed outside the package. If we do not specify and access modifier, it will be marked as default. It is more restrictive than protected.

Let's understand that where access modifier being used by a simple table:

Access Modifier Same Class Same package Subclass Same Package Non-subclass Different package Subclass Different Package Non-subclass
Private Yes No No No No
Default Yes Yes Yes No No
Protected Yes Yes Yes Yes No
Public Yes Yes Yes Yes Yes

Example to illustrate the working of access modifiers.

 classprivateDemo
 {
  int add; // default access
  publicint sub; // public access
  privateintdiv; // private access
  // declaring method to access c
  voidsetdiv(inti) 
  { 
  // setting div's value
  div = i;
  }
 intgetdiv() 
  { 
  // getting div's value
  returndiv;
  }
 }
 classaccessDemo
 {
  publicstaticvoid main(String args[]) 
  {
  privateDemoobj = newprivateDemo();
  obj.add = 10;
  obj.sub = 20;
  // obj.c = 100; // It will give a compile time Error! because of access violation
  // We must access div through its methods
  obj.setdiv(100); // it sets the value of div
 System.out.println("add, sub, and div: " + obj.add + " " +obj.sub + " " + obj.getdiv()); 
  }
 } 

Output:

add, sub, and div: 10 20 100

As we can see, inside the privateDemo class, add uses default access, which is the same as specifying public. sub is explicitly declared as public. And member div is declared private i.e. it cannot be accessed by code outside of the class. So inside the accessDemo class, div can’t be used directly. We accessed it through a public method i.e. setdiv() and getdiv().


Related Topics

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

String isnullorempty in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.