{"meta":{"title":"Simplifying complex inheritance hierarchies","intro":"Copilot Chat can help you to refactor code to avoid classes with multiple layers of inheritance.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot Cookbook"},{"href":"/en/copilot/tutorials/copilot-cookbook/refactor-code","title":"Refactor code"},{"href":"/en/copilot/tutorials/copilot-cookbook/refactor-code/simplify-inheritance-hierarchies","title":"Simplify inheritance hierarchies"}],"documentType":"article"},"body":"# Simplifying complex inheritance hierarchies\n\nCopilot Chat can help you to refactor code to avoid classes with multiple layers of inheritance.\n\nDeep inheritance hierarchies, or improper use of inheritance, can lead to code that is hard to maintain, making it difficult to follow relationships between classes, or to extend them without causing breakage.\n\nCopilot Chat can suggest refactoring inheritance-based designs into more flexible composition-based designs (favoring composition over inheritance). It can also recommend applying patterns like Strategy or Decorator to make the system more extensible without adding complexity to the inheritance structure.\n\n## Example scenario\n\nThe following C# code has a deep inheritance hierarchy where each class builds on the previous one. The result is a long chain of inheritance that makes it difficult to know what properties each class has.\n\nA multi-level inheritance hierarchy like this can be difficult to understand, modify, and extend.\n\n```csharp id=employee-class\npublic class Employee\n{\n    public string Name { get; set; }\n    public int EmployeeID { get; set; }\n}\n\npublic class Manager : Employee\n{\n    public int NumberOfTeams { get; set; }\n}\n\npublic class SeniorManager : Manager\n{\n    public string Region { get; set; }\n}\n\npublic class Director : SeniorManager\n{\n    public int NumberOfDepartments { get; set; }\n}\n\npublic class VicePresident : Director\n{\n    public string Division { get; set; }\n}\n\npublic class President : VicePresident\n{\n    public string Company { get; set; }\n}\n```\n\n## Example prompt\n\nIn this situation you might decide to use a parent `Employee` class whose properties other classes should inherit, but to avoid any other inheritance by defining all other properties separately for each class.\n\n```copilot copy prompt ref=employee-class\nRefactor this code so that classes only inherit the properties of the Employee class. All of the other properties that each class currently inherits should instead be declared independently in each class definition.\n```\n\n## Example response\n\n> \\[!NOTE] The following response is an example. Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.\n\nCopilot suggests the following code:\n\n```csharp\npublic class Employee\n{\n    public string Name { get; set; }\n    public int EmployeeID { get; set; }\n}\n\npublic class Manager : Employee\n{\n    public int NumberOfTeams { get; set; }\n}\n\npublic class SeniorManager : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n}\n\npublic class Director : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n    public int NumberOfDepartments { get; set; }\n}\n\npublic class VicePresident : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n    public int NumberOfDepartments { get; set; }\n    public string Division { get; set; }\n}\n\npublic class President : Employee\n{\n    public int NumberOfTeams { get; set; }\n    public string Region { get; set; }\n    public int NumberOfDepartments { get; set; }\n    public string Division { get; set; }\n    public string Company { get; set; }\n}\n```\n\n## Further reading\n\n* [Prompt engineering for GitHub Copilot Chat](/en/copilot/concepts/prompting/prompt-engineering)\n* [Best practices for using GitHub Copilot](/en/copilot/get-started/best-practices)"}