Every field, method, and constructor in Java has an access level that determines which parts of your codebase can see and use it. Getting access control right is the foundation of encapsulation — one of the four pillars of OOP. When you hide implementation details behind a well-defined public interface, you are free to change the internals later without breaking code that depends on your class. Java provides four access levels, from most open to most restrictive:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/HotCode2025/Print-Estoy-Cansado-Jefe-TercerSemestre/llms.txt
Use this file to discover all available pages before exploring further.
public, protected, package-private (the default, no keyword), and private.
The Four Access Levels at a Glance
| Modifier | Same Class | Same Package | Subclass (any package) | Other Packages |
|---|---|---|---|---|
public | ✅ | ✅ | ✅ | ✅ |
protected | ✅ | ✅ | ✅ | ❌ |
| (default) | ✅ | ✅ | ❌ (if different package) | ❌ |
private | ✅ | ❌ | ❌ | ❌ |
The table shows visibility from the caller’s point of view. A subclass in a different package can access
protected members, but a non-subclass in another package cannot.public — Accessible Everywhere
A public member is visible to any class in any package. Use it for the intentional API of your class — the methods and constructors you want other code to call.
The source prints
"Contructor public" — the word Constructor is missing an ‘s’. This is a typo in the original source file, reproduced here for accuracy. Exposing fields directly as public (like atributoPublic above) is also fine for demonstration, but in production code you should make fields private and expose them through public getters/setters to preserve encapsulation.protected — Package + Subclasses
A protected member is visible within the same package and also to subclasses in any package. This is the right modifier when you want child classes to be able to read or override something, while still keeping it hidden from unrelated code outside the package.
Clase3.java (paquete2 — different package, extends Clase1)
Clase3 lives in paquete2, which is different from paquete1 where Clase1 is defined. Despite the package boundary, Clase3 can still access atributoProtected because the extends relationship grants that privilege.
Package-Private (Default) — Same Package Only
When you write no access modifier at all, the member is package-private (sometimes called default). It is visible to every class in the same package but invisible outside it — even to subclasses in a different package.private — Same Class Only
A private member is accessible only within the class that declares it. This is the strongest restriction and the default you should reach for first when designing a class. Even subclasses cannot directly access a parent’s private fields — they must go through public or protected getters/setters.
Clase4.java (paquete2) — demonstrating private
The Enhanced for-each Loop
Covered alongside access modifiers in Clase 3, the enhanced for loop (also called forEach) provides a cleaner syntax for iterating over arrays and collections without managing an index variable.
Syntax
With a Primitive Array and Objects
TestForEach.java — iterating over primitives and objects
forEach vs. traditional for — when to use which
forEach vs. traditional for — when to use which
| Scenario | Prefer |
|---|---|
| You only need to read each element | for-each — cleaner, less error-prone |
You need the index (e.g., array[i-1]) | Traditional for |
| You need to modify the array element by index | Traditional for |
Iterating a List, Set, or other Iterable | for-each |
for-each loop cannot skip elements or iterate backwards, but for the common case of visiting every element in order it is the preferred style.The Persona class used in the forEach example
The Persona class used in the forEach example
Persona.java (domain package)
Quick Reference
public
Open to everyone. Use for the intentional API: constructors, service methods, and getters/setters.
protected
Open to the same package and to subclasses anywhere. Use for members you want child classes to inherit or override.
default (no keyword)
Open only within the same package. Useful for package-internal helper classes and utilities.
private
Locked to the declaring class. The default choice for all fields and internal helper methods.