{"id":18840789,"url":"https://github.com/lana-20/oop-polymorphism","last_synced_at":"2026-01-27T04:31:58.657Z","repository":{"id":138539845,"uuid":"595969656","full_name":"lana-20/oop-polymorphism","owner":"lana-20","description":"OOP Concept - Polymorphism","archived":false,"fork":false,"pushed_at":"2023-02-07T07:57:56.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-29T21:36:11.907Z","etag":null,"topics":["oop","oop-principles","oops","oops-in-java","oops-in-python"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lana-20.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-01T07:25:19.000Z","updated_at":"2023-02-05T08:27:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"121ebb0e-993d-456e-b5ce-969825abd0b6","html_url":"https://github.com/lana-20/oop-polymorphism","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lana-20/oop-polymorphism","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-polymorphism","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-polymorphism/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-polymorphism/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-polymorphism/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lana-20","download_url":"https://codeload.github.com/lana-20/oop-polymorphism/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-polymorphism/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28802061,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T03:44:14.111Z","status":"ssl_error","status_checked_at":"2026-01-27T03:43:33.507Z","response_time":168,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["oop","oop-principles","oops","oops-in-java","oops-in-python"],"created_at":"2024-11-08T02:49:08.297Z","updated_at":"2026-01-27T04:31:58.642Z","avatar_url":"https://github.com/lana-20.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Polymorhism\n\n__Polymorphism is one of the core concepts of the OOP (Object Oriented Programming).__ Polymorphism is a word composed of two Greek words: _poly_, which means _many_, and _morph_, which means _forms_. Therefore, _polymorphism means many forms_. \n\n### \u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810246-f805f12f-1176-4474-b5cb-cb24b0aa5d88.png\" width=40\u003e COMPILE-TIME Polymorphism / Method OVERLOADING\nMore precisely, in the OOP context, _polymorphism allows an object to behave differently in certain cases_, or, in other words allows an action to be accomplished  in different ways (approaches). _One way to implement polymorphism in Java is via method overloading. This is know as Compile-Time Polymorphism_ because the  compiler can identify at compile time which form of an overloaded method to call (multiple methods with the same name but different arguments). So, depending on which form of the overloaded method is called, the object behaves differently. For example, a class named \u003ccode\u003eTriangle\u003c/code\u003e can define multiple methods named \u003ccode\u003edraw()\u003c/code\u003e with different arguments.\n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810749-64a94f9b-00ad-4d5b-b112-2baa6157bb52.png\" width=40\u003e\n\n    class Triangle {\n        void draw() {\n            System.out.println(\"Drawing a triangle\");\n        }\n\n        void draw(String color) {\n            System.out.println(\"Drawing a \" + color + \" triangle\");\n        }\n\n        void draw(int width, int height) {\n            System.out.println(\"Drawing a triangle with width \" + width + \" and height \" + height);\n        }\n    }\n\n    public class Main {\n        public static void main(String[] args) {\n            Triangle triangle = new Triangle();\n            triangle.draw();\n            triangle.draw(\"red\");\n            triangle.draw(10, 20);\n        }\n    }\n\nUnlike many other popular object-oriented programming languages such as Java, Python doesn’t support compile-time polymorphism or method overloading. If a class or Python script has multiple methods with the same name, the method defined the latest overrides the earlier one.\nPython doesn’t use function arguments for method signature, that’s why method overloading is not supported in Python.\n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810799-021871c1-780a-484d-8634-690968fe9c05.png\" width=40\u003e __Operator Overloading in Python__\n\nPython supports operator overloading. This is another type of polymorphism where an operator behaves differently based on the type of the operands.\n* operator multiplies two numbers and when used with a string and int, repeats the string given int times and concatenates them\n* operator adds two numbers and concatenates two strings\n\nThe below snippet exhibits how to overload the \u003ccode\u003eadd\u003c/code\u003e operator:\n\n        class OperatorOverloading:\n            def__init__(self, pages) :\n                self.pages = pages\n\n            def __add__(self, other):\n                total_pages = self.pages + other.pages\n                return total_pages\n\n        obj1 = OperatorOverloading(10)\n        obj2 = OperatorOverloading(5)\n        print(obj1 + obj2)  # Output: 15\n\nWhen changing \u003ccode\u003e+\u003c/code\u003e to \u003ccode\u003e-\u003c/code\u003e in the \u003ccode\u003etotal_pages\u003c/code\u003e value definition, the result is subtraction in lieu of addition.\n\n        class OperatorOverloading:\n            def__init__(self, pages) :\n                self.pages = pages\n\n            def __add__(self, other):\n                total_pages = self.pages - other.pages\n                return total_pages\n\n        obj1 = OperatorOverloading(10)\n        obj2 = OperatorOverloading(5)\n        print(obj1 + obj2)  # Output: 5\n\n### \u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810338-982cef29-ced4-4cfd-9cd5-072520812118.png\" width=40\u003e RUNTIME Polymorphism / Method OVERRIDING / Dynamic Dispatch\n\n_Another way to implement polymorphism is via method overriding. This is a common approach where there's an IS-A relationship. It is known as Runtime Polymorhism, or Dynamic Dispatch._ Typically, we start with a parent interface/class containing a handful of methods. Next, each child class implements this parental interface/class and overrides these parental methods to provide a specific customized behavior. This time, polymorphism allows us to use any of these children classes exactly like its parent without any confusion of their types. This is possible because, at runtime, Java or Python is smart enough to distinguish among these classes and know which one is used. For example, an interface/class called \u003ccode\u003eShape\u003c/code\u003e can declare a method named \u003ccode\u003edraw()\u003c/code\u003e, and the \u003ccode\u003eTriangle\u003c/code\u003e, \u003ccode\u003eRectangle\u003c/code\u003e, and \u003ccode\u003eCircle\u003c/code\u003e classes implement the \u003ccode\u003eShape\u003c/code\u003e interface/class and override the \u003ccode\u003edraw()\u003c/code\u003e method to draw the corresponding shape.\n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810799-021871c1-780a-484d-8634-690968fe9c05.png\" width=40\u003e\n\n    class Shape:\n        def draw(self):\n            print(\"Drawing a shape\")\n\n    class Triangle(Shape):\n        def draw(self):\n            print(\"Drawing a triangle\")\n\n    class Rectangle(Shape):\n        def draw(self):\n            print(\"Drawing a rectangle\")\n\n    class Circle(Shape):\n        def draw(self):\n            print(\"Drawing a circle\")\n\n    def main():\n        shapes = [Triangle(), Rectangle(), Circle()]\n        for shape in shapes:\n            shape.draw()\n\n    if __name__ == '__main__':\n        main()\n\n\n### Advantages of Polymorphism\n* The scripts and classes written once can be reused and implemented multiple times.\n* It helps in reducing the coupling between different functionalities and behavior of objects.\n\n\u003cimg width=\"800\" src=\"https://user-images.githubusercontent.com/70295997/216844784-b7b015d0-d32f-4935-9267-cc75a4ac1c61.png\"\u003e\n\n\n[Inheritance](https://github.com/lana-20/oop-inheritance), another core OOP concept is essential in supporting Polymorphism in programming.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flana-20%2Foop-polymorphism","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flana-20%2Foop-polymorphism","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flana-20%2Foop-polymorphism/lists"}