Java Notes — Access Modifiers
Exam Style | Easy English | Short Paragraphs + Key Points | With Code Snippets
(Combined from: access [Link] + Access Modifiers word [Link])
0) Why this topic is important
Access modifiers are extremely important in OOP because they are directly related to data
security, encapsulation, data hiding, and good code design.
Helps protect sensitive data (security).
Supports encapsulation and controlled access.
Improves code readability and maintainability.
1) Learning Objectives
After studying this topic, you should be able to define access modifiers, explain why they are
used, list all types, describe visibility scope, write basic code examples, and answer
exam/interview questions.
What access modifiers are and why we use them.
Types: public, private, protected, default (no modifier).
Visibility scope of each modifier.
Coding examples + real-life analogy.
Common mistakes + exam/interview questions.
2) What is an Access Modifier?
An access modifier is a keyword in Java that controls the visibility (accessibility) of a class
member. Class members include variables, methods, constructors, and even classes.
Exam Line: Access modifiers control “who can access what” in a Java program.
3) Real-Life Analogy (House Example)
Think of access modifiers like parts of a house: some areas are open to everyone, some are
limited to family, and some are private only for you.
Drawing room → anyone can enter → public
Bedroom → only family → protected
Personal locker → only you → private
Default → only people “in the same area/house group” → same package
4) Types of Access Modifiers in Java
public
private
protected
default (no modifier)
5) Public Access Modifier
Public is the most open access level. If a member is public, it can be accessed from
anywhere: same class, same package, different packages, and through inheritance.
Exam Line: public = accessible everywhere
Public Code Example
public class Student {
public String name;
public void display() {
[Link]("Name: " + name);
name is public → can be accessed from other classes.
display() is public → can be called from other classes.
6) Private Access Modifier
Private is the most restricted access level. A private member can be accessed only inside the
same class. It is NOT accessible from outside the class, even in the same package or in
subclasses.
Exam Line: private = accessible only within the same class
Private Code Example
class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
public void showBalance() {
[Link](balance);
balance is private → cannot be accessed directly ([Link] ❌).
Access is controlled through methods (deposit(), showBalance()).
Why Use Private?
Data security
Data hiding
Controlled access (encapsulation)
Example: bank balance should not be directly editable.
7) Getters & Setters (Encapsulation Link)
To access private data safely, we use public methods called getters (read data) and setters
(modify data). This is an important part of encapsulation.
public double getBalance() {
return balance;
public void setBalance(double b) {
balance = b;
Getter returns the private value.
Setter updates private value (and can add validation).
8) Protected Access Modifier
Protected lies between public and private. It is accessible within the same class, within the
same package, and in subclasses (even in different packages). But it is not accessible to non-
subclasses outside the package.
Exam Line: protected = same package + subclasses (inheritance friendly)
Protected Code Example
class Animal {
protected void sound() {
[Link]("Animal sound");
class Dog extends Animal {
void bark() {
sound();
Dog is a subclass → it can access protected method sound().
9) Default Access Modifier (No modifier)
If no access modifier is written, the member has default access. Default members are
accessible within the same class and the same package, but not from different packages.
Exam Line: default = package-private (same package only)
Default Code Example
class Test {
int x = 10; // default access
void show() { // default access
[Link](x);
10) Quick Comparison (Exam-Favorite)
Remember this visibility summary for exams (most asked).
public → same class ✅ | same package ✅ | different package ✅ | inheritance ✅
protected → same class ✅ | same package ✅ | different package ✅ (only via subclass) |
inheritance ✅
default → same class ✅ | same package ✅ | different package ❌ | inheritance ✅ only within
package
private → same class ✅ | same package ❌ | different package ❌ | inheritance ❌
11) Practical Scenarios (Think & Answer)
ATM PIN → should be private (highly sensitive).
University name → can be public (not sensitive).
Employee salary → should be private with getters/setters (controlled access).
12) Common Mistakes (Avoid in Exams & Projects)
Making everything public (weak security).
Ignoring data hiding (no encapsulation).
Not using getters/setters for private fields.
Poor OOP design and harder maintenance.
13) Interview / Viva Questions (Short)
Difference between public and protected?
Can private members be inherited?
What is default access in Java?
Why do we use encapsulation (getters/setters)?
14) Coding Exercise (Practice)
Write an Employee class with: private salary, public name, protected department, and
getters/setters for salary.
class Employee {
public String name;
protected String department;
private double salary;
public double getSalary() {
return salary;
public void setSalary(double salary) {
if (salary >= 0) {
[Link] = salary;
15) Short Exam Prompts (From PPT)
Define the primary role of access modifiers in OOP.
What is the scope of a private member?
How does public impact visibility across packages?
Explain protected visibility regarding inheritance.
What is the visibility if no modifier is specified?
16) Conclusion
Access modifiers are essential for secure and maintainable Java programs. They control
visibility, support encapsulation, and help you design clean OOP code.