Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions content/java/concepts/access-modifiers/access-modifiers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
Title: 'Access Modifiers'
Description: 'In Java, access modifiers control who can access classes, methods, or variables, helping protect data and enforce encapsulation. They allow you to hide details, share functionality safely, and organize code for better maintenance.'
Subject:
- 'Computer Science'
Tags:
- 'Access-Modifiers'
- 'OOP'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

# Java Access Modifiers

In Java, **access modifiers** control **who can use a class, method, or variable**.
They are important for **data protection**, **encapsulation**, and **safe code design**.

There are **four types of access modifiers**:


## 1. Public

- `public` is visible to everyone – inside or outside the package.
- Use it when a class or method should be accessible by any other class.
- Think of it as **“open for all”**.


## 2. Private

- `private` is visible only within the same class.
- Use it to hide data or methods from other classes.
- Helps protect your data and enforce encapsulation.


## 3. Protected

- `protected` is visible within the same package and to subclasses in other packages.
- Use it when you want to share functionality with child classes but not everyone.
- Useful in inheritance scenarios.


## 4. Default (Package-Private)

- Default (no modifier) is visible only within the same package.
- Use it for internal package-level access that should not be exposed outside.
- Helps organize related classes without exposing them publicly.


## Comparison Table

| Modifier | Inside Class | Same Package | Subclass (Other Package) | Other Packages |
|--------------|-------------------|-------------------|-----------------------------------|--------------------------|
| **public** | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
| **private** | ✅ Yes | ❌ No | ❌ No | ❌ No |
| **protected** | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
| **default** | ✅ Yes | ✅ Yes | ❌ No | ❌ No |
28 changes: 28 additions & 0 deletions content/java/concepts/loops/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ The output would be:
8
```

## Do-While Loop

The `do-while` loop is similar to a `while` loop, but the code block **executes at least once**, even if the condition is false. This makes it useful when you want the loop to run **at least one time** before checking the condition.

```pseudo
do {
// Code block to be executed
} while (condition);
```

In this example, the code in the loop will run at least once, and then continue running as long as variable i is less than 10:

```java
int i = 5;

do {
System.out.println(i);
i++;
} while (i < 0);

```

The output would be:

```shell
5
```

## For Loop

A `for` loop iterates over a range of values. Its declaration is made up of the following three parts, each separated by a semicolon:
Expand Down
Loading