Skip to main content

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.

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: public, protected, package-private (the default, no keyword), and private.

The Four Access Levels at a Glance

ModifierSame ClassSame PackageSubclass (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.
package paquete1;

public class Clase1 {
    public String atributoPublic = "Valor atributo public";

    public Clase1() {
        System.out.println("Contructor public");
    }

    public void metodoPublico() {
        System.out.println("Método public");
    }
}
Output:
Contructor public
clase1 = Valor atributo public
Método public
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)
package paquete2;

import paquete1.Clase1;

public class Clase3 extends Clase1 {
    public Clase3() {
        super("protected");   // calls Clase1's constructor
        // ✅ Can access protected members inherited from Clase1
        this.atributoProtected = "Accedemos desde la clase hija";
        System.out.println("AtributoProtected = " + this.atributoProtected);
        // ✅ Can also access public members
        this.atributoPublic = "es totalmente publico";
    }
}
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.
Use protected sparingly. It is weaker than private and creates a coupling between a parent class and all its potential subclasses. Prefer exposing behaviour through public template methods and keeping fields private.

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.
package paquete1;

// No 'public' on the class itself — also package-private!
class Clase2 extends Clase1 {
    String atributoDefault = "Valor del atributo default";   // no modifier → package-private

    public Clase2() {
        super();
        this.atributoDefault = "Modificación del atributo default";
        System.out.println("atributoDefault = " + this.atributoDefault);
        this.metodoDefault();
    }

    void metodoDefault() {   // no modifier → package-private
        System.out.println("metodoDefault");
    }
}

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
package paquete2;

public class Clase4 {
    private String atributoPrivate = "atributo privado";

    // Private constructor — no external code can call 'new Clase4()' directly
    private Clase4() {
        System.out.println("Constructor privado");
    }

    // Public constructor delegates to the private one
    public Clase4(String argumento) {
        this();   // ✅ 'this()' can call private constructor within the same class
        System.out.println("Constructor publico");
    }

    // Private method — only usable inside this class
    private void metodoPrivado() {
        System.out.println("Metodo privado");
    }

    // Public getters/setters expose the private field in a controlled way
    public String getAtributoPrivate() {
        return atributoPrivate;
    }

    public void setAtributoPrivate(String atributoPrivate) {
        this.atributoPrivate = atributoPrivate;
    }
}
Start with private. Make a field or method private by default and only relax access when you have a concrete reason. This keeps the public API minimal and makes the class easier to change later without breaking callers.

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

for (ElementType variableName : arrayOrCollection) {
    // use variableName
}

With a Primitive Array and Objects

TestForEach.java — iterating over primitives and objects
package test;

public class TestForEach {
    public static void main(String[] args) {

        // Primitive array — forEach syntax
        int edades[] = {5, 6, 8, 9};
        for (int edad : edades) {
            System.out.println("edad = " + edad);
        }

        // Object array — same clean syntax
        Persona personas[] = {
            new Persona("Juan"),
            new Persona("Carla"),
            new Persona("Beatriz")
        };

        for (Persona persona : personas) {
            System.out.println("persona = " + persona);
        }
    }
}
Output:
edad = 5
edad = 6
edad = 8
edad = 9
persona = Persona{nombre=Juan}
persona = Persona{nombre=Carla}
persona = Persona{nombre=Beatriz}
ScenarioPrefer
You only need to read each elementfor-each — cleaner, less error-prone
You need the index (e.g., array[i-1])Traditional for
You need to modify the array element by indexTraditional for
Iterating a List, Set, or other Iterablefor-each
The 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.
Persona.java (domain package)
package domain;

public class Persona {
    private String nombre;

    public Persona(String nombre) {
        this.nombre = nombre;
    }

    public String getNombre()         { return nombre; }
    public void   setNombre(String n) { this.nombre = n; }

    @Override
    public String toString() {
        return "Persona{" + "nombre=" + nombre + '}';
    }
}

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.

Build docs developers (and LLMs) love