{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1b02146",
   "metadata": {},
   "source": [
    "# Inheritance\n",
    "\n",
    "While the design of OOP inheritance can be complex, the basic idea of inheritance is\n",
    "to **inherit fields and methods** from one class to another (reusability).\n",
    "\n",
    "To achieve inheritance, you design a **base class** and **derived classes**:\n",
    "\n",
    "> - **Derived Class (child)**: the class that inherits from another class\n",
    "> - **Base Class (parent)**: the class being inherited from\n",
    "\n",
    "To inherit from a class, use the `:` symbol.\n",
    "\n",
    "In the example below, the `Car` class (child) inherits the fields and methods from\n",
    "the `Vehicle` class (parent) and the Car class can access the data and actions defined\n",
    "in the Vehicle class: [^inheritance-w3schools]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b0d3ed1",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "class Vehicle                               // base class (parent)\n",
    "{\n",
    "    public string brand = \"Ford\";           // Vehicle field\n",
    "    public void honk()                      // Vehicle method\n",
    "    {\n",
    "        Console.WriteLine(\"Tuut, tuut!\");\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "class Car : Vehicle                         // derived class (child)\n",
    "{\n",
    "    public string modelName = \"Mustang\";    // Car field\n",
    "}\n",
    "\n",
    "\n",
    "class Program\n",
    "{\n",
    "    static void Main(string[] args)         // the Main method\n",
    "    {\n",
    "        Car myCar = new Car();              // Create a myCar object using the \"\"new\" keyword\n",
    "\n",
    "        myCar.honk();                       // Call the honk() method (From the Vehicle class)\n",
    "                                            // on the myCar object\n",
    "\n",
    "        Console.WriteLine(myCar.brand + \" \" + myCar.modelName);\n",
    "                                            // Display the value of the brand field (from the Vehicle class)\n",
    "                                            // and the value of the modelName from the Car class\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1be5a8da",
   "metadata": {},
   "source": [
    "\n",
    "There are three classes in the preceding code: Vehicle, Car, and Program. You have defined\n",
    "a `parent` class `Vehicle` with some fields and methods. When you\n",
    "want to create a class `Car` that would share the same information and actions, instead\n",
    "of repeating the code, you `inherit` the information and actions from Vehicle. Namely,\n",
    "you are creating a `child` class `Car` to reuse the code from the `parent` class.\n",
    "\n",
    "As you can imagine, you may **reuse** `Vehicle` by inheriting it again when you want to\n",
    "create similar `child` class such as `Truck`. That way, you only need to define the fields\n",
    "and methods once in the parent class once, and use them in all the child classes.\n",
    "\n",
    "```{rubric} Footnotes\n",
    "```\n",
    "\n",
    "[^inheritance-w3schools]: [C# Inheritance](https://www.w3schools.com/cs/cs_inheritance.php)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
