Access Modifiers in Java

What are Access Modifiers?

The access modifiers in Java specify the accessibility of a field, constructor, method, or class. We can change the access level of fields, methods, and classes by applying the access modifier to it.

Types Of Access Modifiers:

There are 4 types of access modifiers in Java :

(Least Accessible) private < default < protected < public (Most Accessible)

Public :

  • If a class member is “public” then it can be accessed from anywhere, we are allowing it to the whole world.
  • Method or attribute with this modifier can be accessed from any other class.
  • Keyword: public.

Example:

Java Code

public class SuperPublic {
    public static void publicMethod() {
        System.out.println("Welcome to TUF"); 
    }
}

Private:

  • If a class member is “private” then it will be accessible only inside the same class
  •  Usually, we keep class variables as private and methods that are intended to be used only inside the class as private.
  • Keyword: private.

Example:

Java Code

class TUF{
    // private variable
    private String name;
}

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

        // create an object of TUF
        TUF d = new TUF();

        // access private variable and field from another class
        d.name = "Take You Forward";
    }
}
The above code will give us error :
Main.java:18: error: name has private access in TUF
        d.name = "Take You Forward";

How can we access private variables?

We can use getters and setters to access the private variables.

Example:

Java Code

class TUF {
    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){
        TUF d = new TUF();

        // access the private variable using the getter and setter
        d.setName("Take You Forward");
        System.out.println(d.getName());
    }
}

In this way, we can access the private members, the setter method is used to assign values, and the getter method is used to access the variables.

Default :

  • If a class member doesn’t have any access modifier specified, then it’s treated with default access.
  • This means that method or attribute can be accessed only by classes in the same package.
  • Also, called a private package, which means that all members are visible within the same package but aren’t accessible from other packages:
  • Keyword: no specified keyword.

Example:

Java Code

package defaultPackage;
class TUF{
    void message(){
        System.out.println("This is a default");
    }
}

Protected:

  • If a class member is “protected” then it will be accessible only to the classes in the same package and to the subclasses.
  • Keyword: protected.

Example:

Java Code

class TUF {
    // protected method
    protected void display() {
        System.out.println("I love Take You Forward!😁");
    }
}

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

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

Since protected methods can be accessed from the child classes, we are able to access the method of TUF class from the Student class.

Comparison:

Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article