Access Modifiers in Java

--

In this article, we’ll explain what access modifiers are in Java and how they work.

1. What Are Access Modifiers?

Access modifiers are keywords that define which classes can access specific classes, members, or methods. The available keywords are:

  • public: This class, member, or method can be accessed from everywhere.
  • protected: The class, member, or method can be accessed from classes in the same package and from its children classes.
  • default (This is NOT a keyword, it is the case when the modifier is missing): The class, member, or method can be accessed from classes within the same package.
  • private: This class, member, or method can be accessed only within the same class.

2. How Access Modifiers Work in Java

Firstly, let’s demonstrate how access modifiers work.

2.1 Members accessed inside the declaration class

In order to properly understand that, we have created a simple Person class, with the following attributes:

  • A public member name.
  • A protected member surname.
  • A default member address.
  • A private member ID.
package com.codelearnhub.example.model_person_employee;

public class Person {

public String name;
protected String surname;
String address;
private String ID;

public String getID() {
return ID;
}

public void setID(String ID) {
this.ID = ID;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public static void main(String[] args) {

Person person = new Person();

// We can modify each member directly
// since all the members are in the same class
person.name = "John";
person.surname = "Smith";
person.address = "Thiseos 100, Kallithea";
person.ID = "HJ567900";

// Then print it
System.out.println(person.name);
System.out.println(person.surname);
System.out.println(person.address);
System.out.println(person.ID);

}
}

As you can see, we can access each and every member, since we are in the same class that the members were declared.

2.2 Members Accessed From a Class or a Subclass in the Same Package

Moreover, in this example, we have created 2 new classes, in the same package as Person in the previous example.

  • A Company class.
  • An Employee class, which extends the Person class.

In this case, it doesn’t matter if the class extends the Person class, so we'll only go through the Company class.

In contrast to the example above, what changes here is that the private member name is no longer directly accessible. If you would like to access and modify a private member, you will need to create getter and setter methods with public access modifier, so you can use them everywhere. The rest of the members can still be accessed directly through the ". " operator, since all of them are at least package-private.

package com.codelearnhub.example.model_person_employee;

/**
* Case of class within the same package
*/
public class Company {


public static void main(String[] args) {

Person person = new Person();

// We can modify members with
// public,protected and default access
person.name = "John";
person.surname = "Smith";
person.address = "Thiseos 100, Kallithea";


/*
* We cannot modify directly private members,
* we can only modify them
* through setters
*/

// This won't compile
//person.ID = "HJ567900";
person.setID("HJ567900");

// Then print it
System.out.println(person.name);
System.out.println(person.surname);
System.out.println(person.address);

//We can print it only through getter method
System.out.println(person.getID());

}
}

2.3 Members Accessed From a Class or Subclass in a Different Package

Moving on, in this example, we have created:

  • a School class, which does NOT extend Person, in a package different than that of the Person class.
  • a Student class, which extends Person, in a package different than that of the Person class.

2.3.1 Members Accessed From an Unrelated Class in a Different Package

Now, in this example, we’ll go through how modifiers work in an unrelated School class.

2.3.1.1 Creating a Person Object Inside School Class

        Person person = new Person();

/* We can only access and modify
* name, as it is public
*/
person.name = "John";

// The next 2 assignments won't
// compile, since we are on a different package

//person.surname = "Smith";
//person.address = "Thiseos 100, Kallithea";

/*
* You can only set them
* through setters and
* access them through getters
*/

/*
* This will never compile when
* it is run in a class
* other than Person
*/

//person.ID = "HJ567900";

2.3.1.2 Creating a Student Object Inside School Class

        Student student = new Student();

// This will always work
student.name = "John";

/*
* Since the class School is
* not a subclass of Person,
* whatever applies to Person,
* applies to Student
*/

//As before, these won't compile

// student.surname = "Smith";
// student.address = "Thiseos 100, Kallithea";
// student.ID = "HJ567900";

/*
* You can only set them
* through setters and
* access them through getters
*/

2.3.2 Members Accessed From a Child Class in a Different Package

Lastly, we’ll go through how modifiers work in a child Student class.

When we create a Person object inside Student class, the result will be the same as in School class in the previous section, so there is no need to go through that.

On the other hand, the difference with the previous example is noticeable when creating a Student Object inside Student class.

To demonstrate this, below you can find an example of how that would work:

        Student student = new Student();

// No problem setting this
student.name = "John";

// Now the surname can be set directly
// as the its access is protected

student.surname = "Smith";

// For the other 2 members
// we need setters and getters
// to modify and access them

As you can see, in contrast to School class, we can now access and set the surname member directly.

3. Access Modifiers Table

4. Conclusion

To sum up, you should, now, be able to use modifiers in the most efficient way. You can find the source code of the examples, on my GitHub page.

5. Sources

[1]: Controlling Access to Members of a Class

--

--

Georgios Nikolaos Palaiologopoulos

Experienced Java Developer | Focused on Backend Software Development with Java & Spring Boot | Technical Writer