0% found this document useful (0 votes)
3 views5 pages

Java Access Modifiers New

Uploaded by

dahamamaranath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Access Modifiers New

Uploaded by

dahamamaranath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Access Modifiers

What are Access Modifiers?

Access modifiers in Java are keywords used to control the visibility (access level) of classes, variables, methods, and
constructors.

They help to:

• Protect data
• Implement encapsulation
• Control which parts of a program can access others

Types of Access Modifiers in Java

Java has four access modifiers:

1. public
2. protected
3. default (no keyword)
4. private

Access Level Comparison Table

Modifier Same Class Same Subclass Other


Package (Different Classes
Package)
public
protected
default
private

public Access Modifier

Definition

Members declared as public can be accessed from anywhere.

Rules

• Accessible in all packages


• Mostly used for main methods and APIs

DESHAN RASHMIKA 1
Example

class Student {
public String name;

public void display() {


[Link]("Name: " + name);
}
}

public class Main {


public static void main(String[] args) {
Student s = new Student();
[Link] = "Kamal";
[Link]();
}
}

name and display() are accessible everywhere.

Protected Access Modifier

Definition

protected members are accessible:

• Within the same package


• In subclasses (even in different packages)

Rules

• Used mainly with inheritance

Example (Same Package)

class Person {
protected int age;
}

class Student extends Person {


void showAge() {
[Link](age);
}
}

age is accessible because Student is a subclass.

DESHAN RASHMIKA 2
Default (No Modifier)

Definition

If no access modifier is specified, it is called default access.

Rules

• Accessible only within the same package


• Not accessible outside the package

Example

class Teacher {
String subject; // default access

void showSubject() {
[Link](subject);
}
}

Accessible only inside the same package.

Not accessible from another package.

private Access Modifier

Definition

private members are accessible only within the same class.

Rules

• Most restrictive access level


• Used for data hiding
• Commonly used with getters and setters

Example public class Main {


public static void main(String[] args) {
class BankAccount { BankAccount b = new BankAccount();
private double balance; [Link](5000);
[Link]([Link]());
public void setBalance(double amount) { }
balance = amount; }
}
Direct access to balance is not allowed
public double getBalance() { Access is controlled using methods
return balance;
}

DESHAN RASHMIKA 3
Class Diagrams
Example 1: Student Class (private + public)

Student • + → public
- name : String
+ setName() : void • - → private
+ getName() : String

Example 2: Inheritance with protected

Person
#age: int

Employee
+ showAge() # → protected

Java Modifier Real-Life Example Explanation


public School notice board Anyone can see it
protected Family property Only family members
default Office room Only same office staff
private ATM PIN Only the owner knows

class ATM {

private int pin; // ATM PIN (very secret)

double balance; // Default – bank staff

protected String branch; // Bank + branches

public void withdraw() {} // Anyone with card

DESHAN RASHMIKA 4
What are set and get methods ?

Getter Method (get)

• Purpose: To read or access the value of a private instance variable.


• Naming convention: getVariableName()
• Example: getAge() → returns the value of age.

Setter Method (set)

• Purpose: To set or modify the value of a private instance variable.


• Naming convention: setVariableName(type value)
• Example: setAge(int age) → sets the value of age.

Why use getters and setters?

• In OOP, we follow Encapsulation → data hiding.


• Make instance variables private, so they cannot be accessed directly from outside the class.
• Getters and setters allow controlled access to these variables.

DESHAN RASHMIKA 5

You might also like