Polymorphic code is powerful precisely because a singleDocumentation 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.
Empleado variable can hold a Gerente, an Escritor, or any other subclass. But the flip side is that, from time to time, you need to reach past the parent interface and call a child-specific method — getTipoEscritura() on an Escritor, for example. That is where casting comes in: the controlled conversion of an object reference from one type in the hierarchy to another. Understanding the difference between safe implicit upcasting and the riskier explicit downcasting is essential for writing robust Java programs.
Upcasting and Downcasting
Upcasting — implicit and always safe
Upcasting stores a child object in a parent-type variable. Java performs this automatically because every child is-a instance of its parent; no explicit cast syntax is needed. You lose access to child-specific methods through that reference, but the real object on the heap is unchanged.test/TestConversionObjetos.java — upcasting
Downcasting — explicit and requires a guard
Downcasting goes the other way: from parent type back to child type. You must write the cast explicitly with(ChildType). If the real runtime object is not of that child type, Java throws ClassCastException.
test/TestConversionObjetos.java — downcasting
The Escritor class
Escritor extends Empleado and adds a TipoEscritura field. It overrides both obtenerDetalles() and toString() from the parent chain.
domain/Escritor.java
Safe downcast pattern with instanceof
Safe downcast pattern with instanceof
The safest approach combines Java 16+ offers the pattern-matching shorthand
instanceof with an immediate cast:if (empleado instanceof Escritor e) which binds the cast variable in one step, but the classic form above works in all Java versions.The Object Class — Root of Every Java Class
Every class in Java silently extends java.lang.Object. You never have to write extends Object — the compiler adds it for you when no other parent is declared. This means every object you create already inherits a set of fundamental methods.
Key methods inherited from Object
| Method | Default behaviour | Typical override |
|---|---|---|
toString() | Returns ClassName@hashCode | Return a human-readable representation |
equals(Object) | Compares references (==) | Compare field values |
hashCode() | Memory-address-based integer | Compute from the same fields as equals |
getClass() | Returns the runtime Class object | Usually not overridden |
Reference equality vs. content equality
Without overridingequals, two Empleado objects with the same name and salary are not equal by default — they are two separate objects in memory.
test/TestClaseObject.java
Overriding hashCode and equals
The contract
Java’shashCode/equals contract, which collections like HashMap and HashSet depend on, states:
- If
a.equals(b)istrue, thena.hashCode()must equalb.hashCode(). - If two objects have the same
hashCode, they may or may not be equal (equalsis the final arbiter). - Always override both together — overriding only one breaks the contract and produces mysterious bugs in collections.
Implementation on Empleado
The example below computes hashCode from nombre and sueldo using a prime-number formula, and implements equals by comparing those same fields field by field.
Empleado instances with the same nombre and sueldo will return true from equals and produce the same hashCode — behaving correctly as keys in a HashMap or members of a HashSet.
IDEs like IntelliJ IDEA and NetBeans can auto-generate
hashCode and equals for you via Code → Generate. The generated code follows the same prime-number pattern shown above. Always review the generated fields to make sure only the semantically meaningful ones are included.toString — readable object representation
Overriding toString() on Empleado means that System.out.println(empleado) and string concatenation automatically produce a human-friendly string instead of domain.Empleado@1b6d3586.
domain/Empleado.java — toString