Home >>Java Tutorial >Java Access Modifiers

Java Access Modifiers

Java Access Modifiers

The access modifier in Java basically does the work of specifying the accessibility or scope of a constructor, class, field, or method. The access level of the fields, constructors, class, and methods can be changed by the programmer just by applying the access modifier on it. Apart from the access modifiers, there are generally two types of modifiers in the Java language that are: access modifiers and non-access modifiers.

As far as the Access Modifiers are concerned they are generally of four types in the Java Language as depicted below:

  • Private: The Private access modifier in Java basically has an access level of a private modifier that too only within the class. Since the access of private level hence, it is forbidden from being accesses from outside the class.
  • Default: The Default access modifier in the Java language basically possesses the access level that is only valid within the package. Just like the private access modifier, it cannot be accessed from outside of the package. In case the programmer doesn’t specify any of the access level then by default it will be declared as default access modifier.
  • Protected: The Protected access modifier has an access level that is valid within the package and outside the package via child class. This cannot be accessed from outside of the package, in case the programmer forgets to create the child class.
  • Public: The Public access modifier has an access level that is valid for everywhere. Unlike protected modifiers, this can be accessed from within the class and outside the class and also within the package and outside of the package.

Let’s understand Modifiers and their types in the Java language

The above mentioned Java access modifiers are the parts or you can say the successors of the Java Modifiers. A modifier in Java can be simply understood by its use in the language. It is basically used in order to set the access level for methods, attributes, classes and constructors. There are basically two types of the modifiers in the Java language that are as depicted below:

  • Access Modifiers – These are the modifiers that are used to controls the access level.
  • Non-Access Modifiers – These are the modifiers that are not used to control the access levels instead they deliver other functionality.

1. Access control Modifiers

Java provides a number of access modifiers In order to set the access levels for methods, classes, variables, and constructors, the Java language delivers various access modifiers that are depicted below:

  • The default is the one that is visible to the package and there are no modifiers needed.
  • Private is visible only to the class.
  • Public is visible to the world.
  • Protected is visible to the package and all the subclasses.

2. Non-Access Modifiers

There is numerous of non-access modifiers that are provided by the Java that are basically used to achieve various other functionalities that are depicted below:

  • The static modifier is basically used for creating class methods and the variables.
  • The final modifier is basically used for finalizing the implementations of classes, methods, and variables.
  • The abstract modifier is basically used for creating abstract classes and methods.
  • The synchronized and volatile modifiers are basically used for threads.

Let’s understand the types of the Access modifiers one by one along with their examples:

1. Private

The private access modifier acts just like its name and is accessible only within the class. Here is an example of the private access modifiers in which there are two classes A and Simple are created. In this example a class contains private data member and the private method. The motto of this example is to access these private members from the outside the class so that there is a compile-time error:

class A
{
  
private int data = 40;
   
private void msg ()
  {
    System.out.println ("Hello java");
} 
} 

public class Main
{
  
public static void main (String args[])
  {
    
A obj = new A ();
    
System.out.println (obj.data);	//Compile Time Error  
    obj.msg ();			//Compile Time Error  
} 
} 
Output:
Main.java:9: error: data has private access in A
System.out.println(obj.data);//Compile Time Error
^
Main.java:10: error: msg() has private access in A
obj.msg();//Compile Time Error

2. Default

The Default access modifier possesses the access level that is only valid within the package. The code that has been written is only accessible in the same package. This modifier is generally used when the user don't specify a modifier.

Here is an example of the default access modifiers in the Java language in which the A class will be accessed from outside its package, as of the fact that A class is not public and it cannot be accessed from outside the package:

//save by A.java  
package pack;

class A
{
  
void msg ()
  {
    System.out.println ("Hello");
} 
} 

//save by B.java  
  package mypack;

import pack.*;

class B
{
  
public static void main (String args[])
  {
    
A obj = new A ();		//Compile Time Error  
    obj.msg ();			//Compile Time Error  
} 
} 

3. Protected

The Protected access modifier has an access level that is valid within the package and outside the package via child class. This cannot be accessed from outside of the package, in case the programmer forgets to create the child class. The code will be accessible in the same package and the subclasses.

Here is an example of the protected access modifiers in the Java language in which A class of the pack package is public hence, it can be accessed from outside of the package as of the fact we know that msg method of this package is declared as the protected modifier hence; it can be accessed from outside the class only via inheritance:

//save by A.java  
package pack;

public class A
{
  
protected void msg ()
  {
    System.out.println ("Hello");
} 
} 

//save by B.java  
  package mypack;

import pack.*;

 
class B extends A
{
  
public static void main (String args[])
  {
    
B obj = new B ();
    
obj.msg ();

} 
} 
Output:
Hello

4. Public

The Public access modifier has an access level that is valid for everywhere.
Here is an example of the public access modifiers, observe this example carefully to get to the concept from a depth:

//save by A.java  

 package pack;

public class A
{
  
public void msg ()
  {
    System.out.println ("Hello");
} 
} 

//save by B.java  
  
package mypack;

import pack.*;

 
class B
{
  
public static void main (String args[])
  {
    
A obj = new A ();
    
obj.msg ();

} 
} 
Output:
Hello

No Sidebar ads