> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/ud4-elementos-da-programacion-orientada-a-obxectos/herdanza/modificadores-de-acceso-a-nivel-de-metodo-e-atributo.md).

# Modificadores de acceso a nivel de método e atributo

Implica a que niveis os métodos e os atributos son visibles por outras clases. . Vanse definir os siguientes niveis de visibilidade:

a. **Por defecto**: Visible desde las clases del mismo paquete

b. **Públicos:** Todas las clases pueden acceder

c. **Privados**: Solo se accede desde la propia clase

d. **Protected:** Accede la clase y sus subclases

```java
public class Animal {

    protected String tipo;
    private int idade;

    protected void mostrarTipo() {
        System.out.println("Tipo: " + tipo);
    }

    public void mostrarIdade() {
        System.out.println("Idade: " + idade);
    }
    
    private void outroMetodo(){
        .....
    }
}

public class Paxaro extends Animal {

    public void probarAcceso() {
        this.tipo = "Ave";           // ✔️ permitido (protected)
        this.mostrarTipo();          // ✔️ permitido (protected)

        // this.idade = 3;           ❌ ERROR: private
        this.mostrarIdade();
        // this.outroMetodo();      ❌ ERROR: private
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/ud4-elementos-da-programacion-orientada-a-obxectos/herdanza/modificadores-de-acceso-a-nivel-de-metodo-e-atributo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
