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.
| Modifier | Same Class | Same Package | Subclass | Everywhere |
|---|---|---|---|---|
private | Yes | No | No | No |
| default (no keyword) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
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
EasyLet'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:privatefield:studentId(String) - only accessible within the Student class- default (no modifier) field:
grade(int) - accessible within the same package protectedfield:name(String) - accessible in same package and subclassespublicfield:school(String) - accessible everywhere
publicmethod calledgetInfo()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 theschoolfield directly, then print the result ofgetInfo()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:
| Modifier | Same Class | Same Package | Subclass | Everywhere |
|---|---|---|---|---|
private | Yes | No | No | No |
| default (no keyword) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
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
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System