Java Access Modifiers - The Coding Shala

Home >> Learn Java >> Java Access Modifiers

Java Access Modifiers

Java Modifiers are used to control access mechanisms that mean it defines where we can access the class and its members based on its modifiers. In Java there are two types of modifiers:
  • Access Modifiers
  • Non Access Modifiers
In Java, we have 7 non-access modifiers. Java non-access modifiers are used to provide information about the behavior of class and its members. In Java, static, final, abstract, synchronized, transient, volatile, native are non-access modifiers.
In this chapter, we only discuss Java access Modifiers.
Java Access Modifiers - The Coding Shala


Java Access Modifiers

The Java Access Modifiers specifies the accessibility or we can say they define the scope of a class, constructor, class members, methods, variables. There are four types of Java access modifiers:
  • Default
  • Public
  • Private
  • Protected

Java Default Access Modifiers

When we don't use any access modifier with the class or its members, it is treated as a default access modifier by default. For default access modifiers we don't need to use any access modifier. The default access modifiers are only accessible within the package not outside it.

Example of Java Default Access Modifiers

The following example explains the Java Default access modifier: 

//different package
//save as Main2.java
package Pack2;

class Main2 {
   void msg() {
   System.out.println("Hello from another package");
  }
}

//Package 1 
//save as Main
package Pack1;
import Pack2.*;

class AccessMod{
 void Display() {
  System.out.println("This method has Default access modifiers");
 }
}
class Main {
  public static void main(String[] args) {
   int value = 10;
   System.out.println("This is default class");
   System.out.println("Default variable with value: "+value);
   AccessMod am = new AccessMod();
am.Display();
//Inside another package
//Main2 m = new Main2(); //give compile error //m.msg(); //method not visible compile error } }
Output: 
This is default class
Default variable with value: 10
This method has Default access modifiers

In the above program inside package Pack2, we have declared class and method as default. Whenever we use Pack2 class inside Pack1 we will get a compile-time error.

Java Public Access Modifier

The Java public access modifiers are specified using the public keyword. A class, methods or data members that are declared as public can be accessed from anywhere.

Example of Java Public Access Modifier


The following example explains Java public access modifier: 


//different package
//save as Main2.java
package Pack2;

public class Main2 {
   public void msg() {
   System.out.println("Hello from another package with public access modifier");
  }
}

//Package 1 
//save as Main
package Pack1;
import Pack2.*;

class Main {
  public static void main(String[] args) {
   
   Main2 m = new Main2(); 
   m.msg(); 
   
  }
}
Output: 

Hello from another package with public access modifier

Java Private Access Modifier


The Java private access modifier is specified using keyword private. A class, method or data members that are declared as private can only accessible within the same class in which they are declared not outside that class. 

Example of Java Private Access Modifier


The following example explains Java private access modifiers: 

class PrivateMod{
 private void msg() {
  System.out.println("Hello from private method msg");
 }
}
class Main {
 private void Display() {
  System.out.println("Hello from Same class Display method");
 }
 
public static void main(String[] args) {
 PrivateMod pm = new PrivateMod();
 //pm.msg();  //this will give compile error.. method not visible
 
 Main mm = new Main();
 mm.Display();
}
}
Output: 

Hello from Same class Display method

NOTE: A class cannot be private.

Question: Can we create a private constrctor?
Answer: Yes, we can create a private constructor but when we do that then we can create an object of that class inside that class only not from any other class. The following example explains it: 

class Example{
 private Example() {
  
 }
 
 void msg() {
  System.out.println("Hello there");
 }
 
}

class Main{
 private Main() {}
 
 void Display() {
  System.out.println("Hello form Display");
 }
 
 public static void main(String[] args) {
  //Example ex = new Example();//this will give compile error... constructor not visible
  //ex.msg();  //this will give compile error
  
  Main mm = new Main();
  mm.Display();
 }
}
Output: 

Hello form Display

Java Protected Access Modifier

The Java Protected Access Modifier is specified using the protected keyword. A class, method or data members are declared with a protected access modifier that can accessible within the same class or subclasses in another package.
Note: Class can not be protected.

Example of Java Protected Access Modifiers

The following example explains the java protected access modifiers: 

//different package
//save as Main2.java
package Pack2;

public class Main2 {
   protected void msg() {
   System.out.println("Hello from another package with protecteds access modifier");
  }
}

//Package 1 
//save as Main
package Pack1;
import Pack2.*;

class Main extends Main2 {
  public static void main(String[] args) {
 Main m = new Main();
 m.msg();
  }
}
Output: 

Hello from another package with protecteds access modifier



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala