Tino APCS

Access Modifiers

Some of the fields, methods, and classes we've used are declared with a public keyword in front.

What does public do? Why do we need it? Are there alternatives to public?

The Public Modifier

By adding a public keyword in front, we are declaring that a particular variable, method, or class can be accessed by any other class in any package.

The Package-Private Modifier (No Modifier)

By adding nothing in front, we are declaring that a particular variable, method, or class can be accessed by any other class within the same package,

This means if someone else is importing your code from another package, they can only access the methods, fields and constructors declared with the public access modifier.

The Private Modifier

By adding a private keyword in front, we are declaring that a particular variable, method, or class can only be accessed within the same class. When writing a class, in most cases you should declare fields private and create getters and setters to access and modify them. This gives you the opportunity to protect your class from being broken when a field is set to an invalid value and even gives you the option to only declare a getter, making the field effectively read-only.

The Protected Modifier

The protected access modifier indicates that a field or method can be accessed within the same class, within subclasses of the class or within classes in the same package, but cannot be accessed by any other classes. This keyword is typically used when you want subclasses to still be able to directly use or modify a method or field that is otherwise only supposed to be accessed internally or through getters and setters. Using the protected method also allows classes to declare methods that are meant to be overridden in subclasses, but are not meant to be called by code that is just using the class. We will discuss this more when we discuss inheritance.

Demo

Let's say we have three files "Person.java", "Lab.java" and "Driver.java". The Person and Lab classes are in the experiment package and the Driver is in the default package.

/* Person.java */
package experiment;

public class Person {
    String name;
    public int age;
    private String dna;

    public Person(name, age) { ... }

    private void giveYouUp(){ ... }

    private void letYouDown(){ ... }

    // returns a string containing only the valid characters in str
    protected String removeInvalidChars(String str) {...}

    public void changeDna(String dna){
        this.dna = removeInvalidChars(dna);
    }
}
/* Lab.java */
package experiment;

public class Lab {

    /* fields not shown */

    public Lab() {...}

    // Adds the given person to this lab
    private void addPerson(Person person) { ... }

    public void addRick(String lastName, int age, String dna) {
        Person rick = new Person(lastName, 56);
        rick.name = "Rick " + lastName;  // ok because Lab is in the same package as Person
        rick.dna = "abcdefghijklmnop";  // ERROR: you cannot directly modify a private variable from another class!
        System.out.println(rick.dna);  // ERROR: you cannot directly read a private variable from another class!
        addPerson(rick); // private methods can be called in the same class
    }
}
/* Driver .java */
import experiment.Person;
import experiment.Lab;

public class Driver { 
    public static void main(String[] args){
        Person rick = new Person("Rick", 56);
        rick.name = "Rick Astley";  // ERROR: name is package private and Driver is not in the same package as Person
        rick.age += 1;  // ok because age is public
        rick.giveYouUp();  // ERROR: you cannot use a private method from another class! Plus, rick never gives you up.
        rick.letYouDown();  // ERROR: you cannot use a private method from another class! Plus, rick never lets you down.
        rick.changeDna("qwertyuiop"); // legal because changeDna is a public method
        String validStr = rick.removeInvalidChars("jd7%k3");  // ERROR: Cannot access protected method from Driver (in another package and does not extend Person)

        Lab lab1 = new Lab();
        lab1.addPerson(rick); // ERROR: addPerson is private so it can only be called inside Lab
        lab1.addRick("Roll", 25, "youvebeenrickrolled"); // ok because addRick is a public method in Lab
    }
}

Last modified: September 09, 2023

Back to Getters And Setters

Dark Mode

Outline