Access Modifiers in Java

Role of Access Modifiers in Java:

In our previous tutorial on Java Virtual Machine we saw how JVM helps in running the Java application.

In this tutorial, we will explore the different types of access modifiers that are available in java.

By using the access modifiers, a particular class method /variable can be restricted to access or hidden from the other classes. Learn Java from the core through this excellent series of tutorials.

Here is a Video tutorial on Access Modifiers in Java:

Types of Access Modifiers

Java provides many access modifiers to access the variable, methods, and constructors.

There are 4 types of access variables in Java:

  1. Private
  2. Public
  3. Default
  4. Protected

#1) Private

If a variable is declared as private, then it can be accessed within the class. This variable won’t be available outside the class. So, the outside members cannot access the private members.

Note: Classes and interfaces cannot be private.

#2) Public

Methods/variables with public access modifiers can be accessed by all the other classes in the project.

#3) Protected

If a variable is declared as protected, then it can be accessed within the same package classes and sub-class of any other packages.

Note: Protected access modifier cannot be used for class and interfaces.

#4) Default Access Modifier

If a variable/method is defined without any access modifier keyword, then that will have a default modifier access.

Access ModifiersVisibility
Public Visible to All classes.
ProtectedVisible to classes with in the package and the subclasses of other package.
No Access Modifier (Default)Visible to the classes with the package
privateVisible with in the class. It is not accessible outside the class.

Demo Class:

 class AccessModifiersDemo {

private int empsalaray ;
public String empName;

private void calculateSalary() {
System.out.println("insid methodone");
}

public String printEmpName(String empName ) {
this.empName=empName;
return empName;
}
} 

Java Access Modifiers demo

Accessing the members of the class in another class:

 public class TestAccessModifier {

public static void main(String[] args) {
AccessModifiersDemo accessobj =new
AccessModifiersDemo();

accessobj.calculateSalary();
}
} 

Access Modifiers Test

Output:

Accessing the members of the class in another class- output

Accessing the public members:

 public class TestAccessModifier {

          public static void main(String[] args) {
                        AccessModifiersDemo accessobj =new AccessModifiersDemo();


                        System.out.println(accessobj.printEmpName("Bobby"));

            }

} 

Output:

Bobby

Summary

  • Access modifiers define the visibility of the class.
  • If no keyword is mentioned then that is default access modifier.
  • Four access modifiers in java include public, private, protected and default.
  • Private and Protected keywords cannot be used for classes and interfaces.

Hope you enjoyed the whole bundle of tutorials in this JAVA series. We are sure that these tutorials would have enriched your knowledge of JAVA concept!!

PREV Tutorial