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 8 teaches how Java implements single inheritance using the extends keyword, how subclasses chain constructors with super(), and what happens when you try invalid inheritance patterns. By working through four progressively richer exercises you will see how a subclass inherits fields and methods, adds its own state, calls the parent constructor exactly once as the first statement, and selectively overrides behaviour while still delegating to the parent via super.

Ejercicio 1 — Vehiculo → Coche

Define a Vehiculo base class with protected fields, then extend it with Coche which adds numeroDePuertas and overrides mostrarInfo() by delegating to super.mostrarInfo() before printing its own line.

Ejercicio 2 — Persona → Estudiante

Extend the Persona base class with Estudiante. Demonstrates constructor chaining with super(nombre, edad) and overloaded constructors that accept an optional starting promedio.

Ejercicio 3 — Empleado → Gerente

Override calcularBono() in Gerente to compute a higher bonus (15% base salary + 2% per managed employee) while inheriting all of Empleado’s core fields and behaviour.

Ejercicio 4 — Multiple Inheritance Pitfalls

See the exact compiler error produced by class ClaseC extends ClaseA, ClaseB, understand why private fields are invisible even inside subclasses, and learn the two correct workarounds: interfaces and getters.

Key concepts

ConceptWhat it does
extendsDeclares that a class inherits from exactly one parent class
super()Calls the parent constructor — must be the first statement in the child constructor
super.method()Calls the parent’s version of an overridden method from inside the override
@OverrideAnnotation that tells the compiler to verify the method signature actually matches a parent method
protected fieldsFields that are accessible directly from subclasses (and the same package), unlike private fields which require getters even from child classes
Single-inheritance ruleJava allows extends from only one class per subclass, eliminating the diamond problem

Compile & run commands

All source files live inside numbered packages. Compile from the taller8 directory. Ejercicio 1 — Vehiculo & Coche
cd taller8
javac ejercicio1/Vehiculo.java ejercicio1/Coche.java ejercicio1/PruebaEjercicio1.java
java ejercicio1.PruebaEjercicio1
Ejercicio 2 — Persona & Estudiante
javac ejercicio2/Persona.java ejercicio2/Estudiante.java ejercicio2/PruebaEjercicio2.java
java ejercicio2.PruebaEjercicio2
Ejercicio 3 — Empleado & Gerente
javac ejercicio3/Empleado.java ejercicio3/Gerente.java ejercicio3/PruebaEjercicio3.java
java ejercicio3.PruebaEjercicio3
Ejercicio 4 — Multiple Inheritance
# Compile only the valid files (ClaseC.java intentionally does not compile)
javac ejercicio4/ClaseA.java ejercicio4/ClaseB.java ejercicio4/InterfazB.java \
      ejercicio4/ClaseC_CORRECTO.java ejercicio4/Vehiculo.java \
      ejercicio4/Coche_CORRECTO.java ejercicio4/PruebaEjercicio4.java
java ejercicio4.PruebaEjercicio4

Build docs developers (and LLMs) love