0% found this document useful (0 votes)
1 views

oop

Access modifiers in Java determine the visibility of classes, variables, methods, and constructors, playing a key role in encapsulation. The three main modifiers are `private`, which restricts access to the defining class; `protected`, which allows access within the same package and subclasses; and `public`, which permits access from anywhere in the application. Proper use of these modifiers enhances data security and enforces encapsulation principles in object-oriented programming.

Uploaded by

khawarbashir9090
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)
1 views

oop

Access modifiers in Java determine the visibility of classes, variables, methods, and constructors, playing a key role in encapsulation. The three main modifiers are `private`, which restricts access to the defining class; `protected`, which allows access within the same package and subclasses; and `public`, which permits access from anywhere in the application. Proper use of these modifiers enhances data security and enforces encapsulation principles in object-oriented programming.

Uploaded by

khawarbashir9090
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
You are on page 1/ 5

Understanding Access Modifiers

in Java

Access modifiers in Java play a crucial role in determining the visibility and accessibility
of classes, variables, methods, and constructors in different parts of a program. These
access levels help in implementing encapsulation, an important concept in Object-
Oriented Programming (OOP). The three most commonly used access modifiers are
`private`, `protected`, and `public`.

1. Private Access Modifier:


The `private` keyword restricts access to the members (fields, methods, or constructors) of
a class strictly within that class. In other words, members marked as `private` cannot be
accessed from outside the class, not even by subclasses or classes in the same package.

Example:
Class Person {
Private String name;

Private void printName() {


System.out.println(“Name: “ + name);
}
Public void setName(String name) {
This.name = name;
}

Public void display() {


printName(); // Can be accessed within the class
}
}

Public class Main {


Public static void main(String[] args) {
Person p = new Person();
p.setName(“John”);
p.display(); // Correct, accessing public method
// p.printName(); // Error, printName() is private
}
}
In this example, the `name` variable and `printName()` method are `private`, meaning they
cannot be accessed directly from outside the `Person` class. They can only be accessed
through public methods like `display()`.
2. Protected Access Modifier:
The `protected` keyword allows members to be accessed within the same package and by
subclasses (even if the subclass is in a different package). This provides a broader scope
than `private` but more restricted than `public`.

Example:
Class Animal {
Protected String type;

Protected void makeSound() {


System.out.println(“Animal sound!”);
}
}

Class Dog extends Animal {


Public void bark() {
System.out.println(“The dog is barking…”);
makeSound(); // Accessible because it’s protected
}
}

Public class Main {


Public static void main(String[] args) {
Dog dog = new Dog();
Dog.bark(); // Works, bark() accesses the protected method
}
}

Here, the `makeSound()` method is `protected`, so it can be accessed in the `Dog` class
(which is a subclass of `Animal`).

3. Public Access Modifier:


The `public` keyword allows the widest scope of access. Members declared as `public` are
accessible from anywhere in the application, whether they are in the same class, package,
or an entirely different package.

Example:
Class Car {
Public String model;

Public void printModel() {


System.out.println(“Car model: “ + model);
}
}
Public class Main {
Public static void main(String[] args) {
Car car = new Car();
Car.model = “Tesla Model X”;
Car.printModel();
}
}

In this example, both `model` and `printModel()` are `public`, so they can be accessed
from anywhere, including outside the `Car` class.

Conclusion:
The use of `private`, `protected`, and `public` access modifiers is essential for controlling
access to the data and behavior of objects in Java. Choosing the right access level helps in
securing data, reducing unintended interactions, and enforcing the principles of
encapsulation in object-oriented programming.

You might also like