# error: records cannot extend classes

- **ID:** `java/records-cannot-extend-class`
- **Domain:** java
- **Category:** compilation_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

Java records implicitly extend java.lang.Record and cannot extend any other class because they are final and their superclass is predetermined.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 16 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## Workarounds

1. **Convert the record to a regular class that extends the desired class and manually implements equals, hashCode, toString, and a constructor. Example: `public class MyClass extends BaseClass { private final int x; public MyClass(int x) { this.x = x; } // plus equals/hashCode/toString }`** (90% success)
   ```
   Convert the record to a regular class that extends the desired class and manually implements equals, hashCode, toString, and a constructor. Example: `public class MyClass extends BaseClass { private final int x; public MyClass(int x) { this.x = x; } // plus equals/hashCode/toString }`
   ```
2. **Use composition instead of inheritance: have the record contain a field of the base class type. Example: `public record MyRecord(BaseClass base, int x) { }`** (95% success)
   ```
   Use composition instead of inheritance: have the record contain a field of the base class type. Example: `public record MyRecord(BaseClass base, int x) { }`
   ```
3. **If you need the base class functionality, create an interface that the base class implements, and have the record implement that interface instead.** (85% success)
   ```
   If you need the base class functionality, create an interface that the base class implements, and have the record implement that interface instead.
   ```

## Dead Ends

- **Remove the 'record' keyword and make it a regular class that extends the desired class.** — This loses all record features (canonical constructor, equals/hashCode/toString), requiring manual implementation and breaking serialization behavior. (70% fail)
- **Use 'implements' instead of 'extends' on the record.** — Records can implement interfaces but not extend classes; the error message is about extending classes, not interfaces. (90% fail)
- **Make the record abstract to allow extending.** — Records cannot be abstract; the compiler will reject with 'illegal combination of modifiers: abstract and final'. (100% fail)
