Menu
Coddy logo textTech

Access Levels Overview

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 10 of 87.

Access modifiers control where fields and methods can be accessed from. Java has four levels of access control.

ModifierSame ClassSame PackageSubclassEverywhere
privateYesNoNoNo
default (no keyword)YesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes
public class BankAccount {
    private double balance;      // Only this class
    String accountType;          // Default: same package only
    protected String owner;      // Same package + subclasses
    public String bankName;      // Accessible everywhere
}

private is the most restrictive - use it for internal data that should never be accessed directly. public is the most open - use it for methods that other classes need to call.

When you don't specify any modifier, Java uses default (also called package-private) access. This allows access only within the same package.

class Helper {           // Default access class
    void assist() { }    // Default access method
}

Choose the most restrictive access level that still allows your code to work. This protects your data and makes your code easier to maintain.

challenge icon

Challenge

Easy

Let's build a Student record system that demonstrates how access modifiers control visibility across different files.

You'll create two files to organize your code:

  • Student.java: Define a Student class with fields using different access levels:
    • private field: studentId (String) - only accessible within the Student class
    • default (no modifier) field: grade (int) - accessible within the same package
    • protected field: name (String) - accessible in same package and subclasses
    • public field: school (String) - accessible everywhere
    Include a constructor that sets all four fields, and a public method called getInfo() that returns a string in the format: "[studentId] name - Grade grade at school"
  • Main.java: Create a Student object and demonstrate which fields can be accessed directly from outside the class. Print the school field directly, then print the result of getInfo() to show all the student's information.

You will receive four inputs: studentId, name, grade, and school (in that order).

Your output should be two lines: the school name accessed directly, followed by the full student info from the getInfo() method.

Cheat sheet

Access modifiers control where fields and methods can be accessed from. Java has four levels of access control:

ModifierSame ClassSame PackageSubclassEverywhere
privateYesNoNoNo
default (no keyword)YesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes
public class BankAccount {
    private double balance;      // Only this class
    String accountType;          // Default: same package only
    protected String owner;      // Same package + subclasses
    public String bankName;      // Accessible everywhere
}

private is the most restrictive - use it for internal data that should never be accessed directly. public is the most open - use it for methods that other classes need to call.

When you don't specify any modifier, Java uses default (also called package-private) access, which allows access only within the same package:

class Helper {           // Default access class
    void assist() { }    // Default access method
}

Choose the most restrictive access level that still allows your code to work. This protects your data and makes your code easier to maintain.

Try it yourself

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Read inputs
        String studentId = scanner.nextLine();
        String name = scanner.nextLine();
        int grade = Integer.parseInt(scanner.nextLine());
        String school = scanner.nextLine();
        
        // TODO: Create a Student object with the input values
        
        
        // TODO: Print the school field directly (accessible because it's public)
        
        
        // TODO: Print the result of getInfo() to show all student information
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming