You are currently viewing Java Access Modifiers with Example

Java Access Modifiers with Example

In this tutorial, we’ll learn about the Java Access Modifier/access modifiers in java, their types, and how to use them in this tutorial.

What are Access Modifiers?

The accessibility or scope of a field, method, constructor, or class is defined by the access modifiers in Java. The access modifier can be used to change the access level of fields, constructors, methods, and classes.

  • Modifiers in Java are divided into two categories: access modifiers and non-access modifiers.
  • In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors, data members, and the setter methods.

Example:

class Demo {
public void sample() {
//…
}
private void sample2() {
//…
}
}

We’ve declared two methods in the example above: sample() and sample2(). Here,

  • sample is public, which means that other classes can use it.
  • sample2 is private, which means that no other classes can access it.
  • Take note of the terms “public” and “private.” In Java, these are called access modifiers. Visibility modifiers are another name for them.

Types of Access Modifier

In Java, there are four keywords for access modifiers:

ModifierDescription
DefaultOnly the package’s declarations are visible (package private)
PrivateDeclarations are only visible within the class.
ProtectedDeclarations are visible throughout the package or all subclasses.
PublicDeclarations can be found everywhere.

Java Access Modifiers Explained

Let’s look at a simple table to understand access modifiers in Java.

Access Modifierwithin the classwithin the packageonly by subclasses outside of the packageoutside package
PrivateYesNo NoNo
DefaultYes YesNoNo
ProtectedYesYesYesNo
PublicYesYesYesYes

Default Access Modifier

If no modifier is used, it is treated as default by default. The default modifier is only available within the package. It is not accessible from outside the package. It is more accessible than a private residence. However, it is more restrictive than protected and open.

If no access modifier is explicitly specified for classes, methods, variables, and so on, the default access modifier is used. As an example:

package default package;
class Sample {
void setdata(){
System.out.println(“DevelopersDome”);
}
}

The Sample class has the default access modifier in this case. Furthermore, the class is visible to all classes in the defaultPackage package. If we try to use the Sample class in a class other than defaultPackage, we will get a compilation error.

Private Access Modifier

The private access modifier is only available within the class. Variables and methods declared private cannot be accessed outside of the class.

Example:

class Sample {
    // private var
    private String Name;
}

public class Main {
    public static void main(String[] main){

        // create object of Sample
        Sample s = new Sample();

        // access private variable and field from another class
        s.name = "DevelopersDome";
    }
}

In the preceding example, we declared a private variable named Name as well as a private method called display(). When we run the program, we will receive the following error message:

Name has private access in Sample
s.name = “DevelopersDome”;
^
  • The error occurs because we are attempting to access the Sample class private variable and private method from the Main class.
  • You’re probably wondering what happens if we need to access those private variables. We can use the getters and setters method in this case. As an example:
class Sample {
    private String Name;

    // getter method
    public String getName() {
        return this.Name;
    }
    // setter method
    public void setName(String Name) {
        this.Name= Name;
    }
}
public class Main {
    public static void main(String[] main){
        Sample s = new Sample();

        // access the private variable using the getter and setter
        s.setName("DevelopersDome");
        System.out.println(s.getName());
    }
}

Output:

The name is DevelopersDome

  • We have a private variable called Name in the preceding example. We used the getName() and setName() methods to access the variable from the outer class (). In Java, these methods are known as getter and setter.
  • The setter method (setName()) was used to assign a value to the variable, and the getter method (getName()) was used to access the variable.

Protected Access Modifier

The protected access modifier is accessible both within and outside of the package, but only through inheritance.

  • The protected access modifier is applicable to data members, methods, and constructors. It cannot be used on the class.
  • It’s more accessible than the default modifer.
  • When we declare methods and data members as protected, we can access them both within the same package and from subclasses.

Example:

class Student {
    // protected method
    protected void display() {
        System.out.println("I am an student");
    }
}

class Teacher extends Student {
    public static void main(String[] args) {

        // create an object of Teacher class
        Teacher  teach  = new Teacher ();
         // access protected method
        teach.display();
    }
}

Output:

I am an student

In the preceding example, the Student class contains a protected method named display(). The Teacher class inherits the Student class.

  • We then created a Teacher class object teach. We attempted to access the protected method of the parent class using the object.
  • Because protected methods can be accessed from child classes, we can access the Student class’s method from the Teacher class.

anon access modifiers in java, default access modifier in java, what is access modifier in java, what are access modifiers in java,access specifiers and protected access modifier in java, native access modifier in java, difference between access specifiers and access modifiers in java, what are the access modifiers in java,access modifiers in java, non access modifiers in java, default access modifier in java, what is access modifier in java, what are access modifiers in java,access specifiers a, protected access modifier in java, native access modifier in java, difference between access specifiers and , what are the

Public Access Modifier

The public access modifier can be accessed from anywhere. It has the broadest scope of any modifier. When we declare methods, variables, classes, and so on as public, we can access them from anywhere. The scope of the public access modifier is unrestricted.

Example:

public class Student{
    // public variable
    public int book;

    // public method
    public void display() {
        System.out.println("I am an student");
        System.out.println("I have " + book);
    }
}

// Main.java
public class Main {
    public static void main( String[] args ) {
        // accessing the public class
        Student student = new Student();

        // accessing the public variable
        student.book = Maths Book;
        // accessing the public method
        student.display();
    }
}

Output:

I am an student
I have Maths Book
  • The public class Student is accessed from the Main class.
  • The public variable book is accessed from the Main class.
  • The public method display() is accessed from the Main class.

You may like:

Constructor in java with Example

Hope this article will guide you to recognize all about the Java Access Modifiers with Example that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

Leave a Reply