Polymorphism is widely considered the most powerful concept in object-oriented programming. It lets you write a single method that accepts a parent type and then automatically executes the correct behaviour for whatever child type is actually passed at runtime — noDocumentation 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.
if/else chains based on class names, no manual dispatch tables. When combined with method overriding, polymorphism lets your code be both flexible and closed for modification: you add new behaviour by adding new subclasses, not by editing existing code.
Method Overriding with @Override
Overriding lets a subclass replace a method it inherited from its parent with its own version. The method signature — name, parameter list, and return type — must match exactly. Java resolves which version to call at runtime based on the actual object type, not the declared variable type.
Rules for overriding
- The method name and parameter list must be identical to the parent’s.
- The return type must be the same (or a covariant sub-type).
- The access modifier cannot be more restrictive than the parent’s.
- Use
super.methodName()inside the override to call the parent implementation before extending it.
The
@Override annotation is technically optional — the program compiles without it. However, it is strongly recommended because it asks the compiler to verify that you are actually overriding an inherited method. Without it, a typo in the method name silently creates a new method instead of an override, which is a notoriously hard bug to spot.Building the Empleado / Gerente hierarchy
Define the parent class with a base method
Empleado exposes obtenerDetalles() that returns the employee’s name and salary. Every subclass will override this method.domain/Empleado.java
Override the method in the subclass
Gerente extends Empleado and overrides obtenerDetalles(). It calls super.obtenerDetalles() to reuse the parent’s logic and then appends the department.domain/Gerente.java
Polymorphism: Parent-Type Variables at Runtime
Polymorphism means “many forms”. In Java, a variable declared as a parent type can hold any object of that type or any of its subclasses. Which method actually runs is determined at runtime by the real type of the object stored in the variable.Storing a child in a parent-type variable
test/TestSobreescritura.java
How dynamic dispatch works
| Variable declared as | Object actually stored | Method called at runtime |
|---|---|---|
Empleado | new Empleado(...) | Empleado.obtenerDetalles() |
Empleado | new Gerente(...) | Gerente.obtenerDetalles() |
Type Checking with instanceof
Before you downcast a parent-type reference to a specific child type, always verify the real type with instanceof. Skipping this check risks a ClassCastException at runtime.
Basic instanceof usage
test/TestSobreescritura.java
Always check for the most specific (child) type first. Because every
Gerente is also an Empleado, placing the Empleado check first would match both and you would never reach the Gerente branch.Safe downcast after instanceof — the Empleado exercise
Once instanceof confirms the real type, the cast is safe and you can access child-only members such as getDepartamento().
test/TestSobreescritura.java
Why check instanceof before every downcast?
Why check instanceof before every downcast?
A reference variable of type
Empleado can hold any subclass at runtime. If you blindly cast to Gerente when the object is actually a plain Empleado, Java throws ClassCastException and your program crashes. The instanceof guard eliminates that risk by confirming the real type before the cast executes.