Sometimes a class represents a concept so general that it makes no sense to create an instance of it directly. A “shape” has an area — but what is the area of a shape in the abstract? The calculation is different for a circle, a rectangle, and a triangle. Abstract classes exist precisely for this situation: they define the contract (every shape must be able to calculate its area) while leaving the implementation to each concrete subclass. This combination of structure and flexibility is the foundation of many powerful design patterns.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.
What an Abstract Class Is
An abstract class is declared with theabstract keyword on the class definition. Two things immediately follow:
- You cannot create an instance of it with
new. - It can declare abstract methods — method signatures with no body — that every non-abstract subclass must implement.
abstract, passing the obligation down the chain.
The abstract Keyword on Methods
An abstract method has a signature but no implementation block — just a semicolon:
calcularArea() has been implemented. This is the contract: you can write code that calls calcularArea() on any FiguraT11 reference without knowing which concrete subclass it is, and the right implementation will run.
FiguraT11 — The Abstract Base Class (Taller 11)
The Figura class from Taller 11 demonstrates the pattern cleanly. It declares one abstract method (calcularArea()) and one concrete method (mostrarArea()) that calls it:
mostrarArea() is written once. It calls calcularArea() which, at runtime, dispatches to whichever subclass is actually in memory. This is the power of combining abstraction with polymorphism.
Concrete Subclasses: CirculoT11 and RectanguloT11
Each shape provides its own formula for calcularArea():
MainEjercicio1T11, both shapes are used through FiguraT11 references — the same mostrarArea() call produces geometrically correct output for each:
Abstract Class vs. Interface
Both abstract classes and interfaces define contracts, but they serve different purposes:| Feature | Abstract class | Interface |
|---|---|---|
| Instantiation | ❌ Cannot instantiate | ❌ Cannot instantiate |
| Constructors | ✅ Can have constructors | ❌ No constructors |
| Instance fields | ✅ Can have instance fields | ❌ Only constants (static final) |
| Concrete methods | ✅ Can have concrete methods | ✅ default methods (Java 8+) |
| Inheritance limit | One parent class | Implement multiple interfaces |
| Best used for | A shared base type with common state | A behavioral role/capability |
You cannot do
new FiguraT11(...) — the compiler will reject it. Instantiate the concrete subclass instead.