DEV Community

Lalit Kumar
Lalit Kumar

Posted on

What is the difference between public, protected, package-private and private in Java?

These keywords (except package-private) are used to manage access or visibility of class attributes (which represent data) to other classes that interact with the class that contains the attribute(s). So what are the major differences between these access modifiers?


title: "What is the difference between public, protected, package-private and private in Java?"
tags: java

canonical_url: https://kodlogs.com/blog/37/difference-between-public-protected-package-private-private

In short, members that are:

private - accessible to the class only.
package-private - accessible to the class and package
protected - accessible to class, package, and subclasses
public - accessible to every other class

private variables or methods are accessible only within the class in which they are declared. Mostly it is recommended that programmers start with this level of visibility for any variable and make adjustments as necessary. It is viewed as good coding practice to initially declare a variable private. Another important point is that private methods can not be called from outside the class they are declared in. They are part of a class’s internal logic that you don’t want to expose. Look at this example:

class Person {
    private String name = "Java";
    private int age = 12;
}

class Company {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);
        // the above print statement will throw an exception
        // because an attempt to access a private member outside
        // of the class is being made
    }
}
Enter fullscreen mode Exit fullscreen mode

package-private visibility is also part of Java’s access levels. package-private is not a keyword in Java. There is no keyword for it in the language but it is the default access level for any variable declared without the modifiers private, protected or public. It is also refered to as the default access level. A class, variable or method that has no modifier (which inherently becomes a default attribute) is only accessible to the class itself and all other classes that are in the same package as the class hence the name “package-private”.
protected members are not much different from default members. But they do stand out considering the fact that they are also accessible outside of package on a subclass. A subclass is any class that extends a class (the parent class). If, for example, the parent class has a protected inner class, then the extending class (subclass) will have access to this inner class. So the access level of protected classes is the class itself, the package and outside the package for extending classes.
package a.b;

public class Person {

    protected void sayHi() {
        System.out.println("Hi!");
    }

}

// ...

package c.d;

import a.b.Person;

class Employee extends Person {

    public static void main() {
        Employee employee = new Employee();
        employee.sayHi();
        // even though the method sayHi() is protected, the call above
        // will print out the message "Hi" without any errors as Employee
        // extended Person
    }

}
Enter fullscreen mode Exit fullscreen mode

Finally there is the public modifier. A public class is accessible from anywhere within the codebase—inside the class, inside the package and outside the package. Everyone has access to such classes. There are articles out there that condemb the pratice of making an attribute public. They say it is better to make it private and prepare setter or getter methods for it. The choice is up to you the developer.

Top comments (0)