Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Jhaymayleth/unidad2_java/llms.txt

Use this file to discover all available pages before exploring further.

Taller 9 focuses on the super keyword — how to call a parent class constructor from a subclass constructor, how to invoke an overridden parent method from within a subclass method, and what the Java compiler rejects when super is used incorrectly. Working through these three exercises gives you a concrete picture of how the JVM wires parent and child class initialisation together and where the boundaries of super lie.

Ejercicio 1: Persona & Empleado

Chain constructors between Persona and Empleado using super(nombre, edad), then delegate to the parent’s mostrarDetalles() via super.mostrarDetalles().

Ejercicio 2: Animal & Pez

Extend Animal with Pez, passing the especie argument up with super(especie) and enriching the output with the subclass-specific tipoDeAgua field.

Ejercicio 3: super Pitfalls

See two deliberate compile-time errors: using super inside a static method and trying to reach a private parent field directly through super.

Key Concepts

super() in constructors

When a subclass constructor calls super(...), it delegates the initialisation of the inherited fields to the parent constructor. This call must be the first statement in the subclass constructor.
public Empleado(String nombre, int edad, String departamento) {
    super(nombre, edad); // must come first — initialises Persona's fields
    this.departamento = departamento;
}

super.method() in instance methods

Inside an overriding instance method you can still run the parent’s version of that method by prefixing the call with super.:
@Override
public void mostrarDetalles() {
    super.mostrarDetalles(); // runs Persona.mostrarDetalles()
    System.out.println("Departamento: " + departamento);
}

Why super fails in a static context

super is a reference to the parent portion of the current object (this). Static methods do not have a this reference, so super is meaningless there and the compiler rejects it:
error: non-static variable super cannot be referenced from a static context

Private vs protected members

ModifierSubclass can read via super?
public✅ Yes
protected✅ Yes
private❌ No — use a getter

Compile & Run

All classes live in the Taller9 package. From the project root (the directory that contains the Taller9/ folder):
# Compile all sources for this taller
javac Taller9/*.java

# Run each main class
java Taller9.MainEjercicio1
java Taller9.MainEjercicio2
java Taller9.MainEjercicio3

Build docs developers (and LLMs) love