One of the most consequential design decisions in any Java project is choosing between an abstract class and an interface. Both tools let you define a contract that subclasses or implementing classes must fulfil, but they do so for different reasons and with different trade-offs. Abstract classes model shared identity — aDocumentation 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.
FiguraGeometrica that every concrete shape really is — while interfaces model shared capability — things a class can do, regardless of where it sits in the inheritance tree. Getting this choice right leads to code that is easy to extend; getting it wrong leads to brittle hierarchies that resist change.
Abstract Classes
The abstract keyword
An abstract class is a class that cannot be instantiated directly. You declare it with the abstract modifier. It can contain:
- Abstract methods — declared with
abstract, no body, must be overridden by every concrete subclass. - Concrete methods — fully implemented, inherited as-is (or overridden) by subclasses.
- Fields and constructors — abstract classes can hold state and define constructors that subclasses call via
super(...).
FiguraGeometrica — a shape hierarchy
FiguraGeometrica declares an abstract method dibujar(). Every shape (circle, rectangle, triangle…) must know how to draw itself, but the how is specific to each shape — there is no sensible default implementation.
domain/FiguraGeometrica.java
Attempting
new FiguraGeometrica("Círculo") produces a compile error: FiguraGeometrica is abstract; cannot be instantiated. You must always instantiate a concrete subclass and, if needed, store it in a FiguraGeometrica reference (upcasting).Implementing dibujar() in Rectangulo
Rectangulo extends FiguraGeometrica and provides a concrete body for dibujar(). The @Override annotation confirms that it is satisfying the abstract contract.
domain/Rectangulo.java
Rectangulo in a FiguraGeometrica variable and calls dibujar() through the abstract reference:
test/TestAbstractas.java
What happens if a subclass does not implement all abstract methods?
What happens if a subclass does not implement all abstract methods?
If a concrete class extends an abstract class but does not override every abstract method, the compiler refuses to compile it and reports
Rectangulo is not abstract and does not override abstract method dibujar() in FiguraGeometrica. The only legal alternative is to declare the subclass abstract as well, pushing the obligation down to its own children.Interfaces
Interface syntax and rules
An interface is a pure contract. Before Java 8, every method in an interface was implicitlypublic abstract and every field was implicitly public static final. That is still the dominant pattern used in courses like this one.
Key characteristics:
- Declared with the
interfacekeyword. - All methods are
public abstractby default — no body (unless using Java 8+defaultmethods). - All fields are
public static finalconstants. - A class can
implementmultiple interfaces simultaneously. - Interfaces cannot be instantiated directly.
IAccesoDatos — a CRUD contract
IAccesoDatos defines the four operations every data-access object must support: insert, list, update, and delete. The constant MAX_REGISTRO is implicitly public static final.
accesodatos/IAccesoDatos.java
Implementing Interfaces
A class declares that it satisfies an interface’s contract using theimplements keyword. The compiler enforces that every method listed in the interface has a concrete implementation in the class (or in one of its parents).
ImplementacionMySql
accesodatos/ImplementacionMySql.java
ImplementacionOracle
accesodatos/ImplementacionOracle.java
Swapping implementations at runtime
Because both classes implement the same interface, you can store either one in anIAccesoDatos variable and switch seamlessly — a textbook example of the Strategy pattern.
test/TestInterfaces.java
Abstract Class vs. Interface — Comparison
| Feature | Abstract Class | Interface |
|---|---|---|
| Can hold instance fields (state)? | ✅ Yes | ❌ No (only static final constants) |
| Can have constructors? | ✅ Yes | ❌ No |
| Can provide concrete methods? | ✅ Yes | ⚠️ Only with Java 8+ default/static |
| Multiple inheritance? | ❌ Single parent only | ✅ A class can implement many interfaces |
| Instantiable directly? | ❌ No | ❌ No |
| Access modifiers on members? | Any (public, protected, private) | Implicitly public |
| When to use | Shared identity + partial implementation | Shared capability / contract |