{
  "id": "java/records-cannot-extend-class",
  "signature": "error: records cannot extend classes",
  "signature_zh": "错误：记录不能继承类",
  "regex": "records cannot extend classes",
  "domain": "java",
  "category": "compilation_error",
  "subcategory": null,
  "root_cause": "Java records implicitly extend java.lang.Record and cannot extend any other class because they are final and their superclass is predetermined.",
  "root_cause_type": "generic",
  "root_cause_zh": "Java 记录隐式继承 java.lang.Record，不能继承其他任何类，因为它们是 final 的且超类是预定的。",
  "versions": [
    {
      "version": "Java 16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 17",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Remove the 'record' keyword and make it a regular class that extends the desired class.",
      "why_fails": "This loses all record features (canonical constructor, equals/hashCode/toString), requiring manual implementation and breaking serialization behavior.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Use 'implements' instead of 'extends' on the record.",
      "why_fails": "Records can implement interfaces but not extend classes; the error message is about extending classes, not interfaces.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Make the record abstract to allow extending.",
      "why_fails": "Records cannot be abstract; the compiler will reject with 'illegal combination of modifiers: abstract and final'.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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 }`",
      "success_rate": 0.9,
      "how": "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 }`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use composition instead of inheritance: have the record contain a field of the base class type. Example: `public record MyRecord(BaseClass base, int x) { }`",
      "success_rate": 0.95,
      "how": "Use composition instead of inheritance: have the record contain a field of the base class type. Example: `public record MyRecord(BaseClass base, int x) { }`",
      "condition": "",
      "sources": []
    },
    {
      "action": "If you need the base class functionality, create an interface that the base class implements, and have the record implement that interface instead.",
      "success_rate": 0.85,
      "how": "If you need the base class functionality, create an interface that the base class implements, and have the record implement that interface instead.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "将记录转换为继承所需类的普通类，并手动实现 equals、hashCode、toString 和构造函数。示例：`public class MyClass extends BaseClass { private final int x; public MyClass(int x) { this.x = x; } // 加上 equals/hashCode/toString }`",
    "使用组合而非继承：让记录包含基类类型的字段。示例：`public record MyRecord(BaseClass base, int x) { }`",
    "如果需要基类功能，创建一个基类实现的接口，然后让记录实现该接口。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.oracle.com/en/java/javase/17/language/records.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.95,
  "resolvable": "true",
  "first_seen": "2024-05-22",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}