{"id":18840797,"url":"https://github.com/lana-20/oop-encapsulation","last_synced_at":"2026-02-23T15:31:30.765Z","repository":{"id":138539838,"uuid":"598864262","full_name":"lana-20/oop-encapsulation","owner":"lana-20","description":"Encapsulation binds together the code and data in a single unit of work (a class) and acts as a defensive shield that doesn’t allow the external code to access this data directly. ","archived":false,"fork":false,"pushed_at":"2023-02-08T03:28:10.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-29T21:36:15.158Z","etag":null,"topics":["data-hiding","encapsulation","encapsulation-protocol","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-08T00:33:19.000Z","updated_at":"2023-02-08T01:16:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"5b55d3e6-a184-4eb7-9715-2e997e06300c","html_url":"https://github.com/lana-20/oop-encapsulation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lana-20/oop-encapsulation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-encapsulation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-encapsulation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-encapsulation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-encapsulation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lana-20","download_url":"https://codeload.github.com/lana-20/oop-encapsulation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lana-20%2Foop-encapsulation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29746539,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-23T07:44:07.782Z","status":"ssl_error","status_checked_at":"2026-02-23T07:44:07.432Z","response_time":90,"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":["data-hiding","encapsulation","encapsulation-protocol","oop","oop-principles","oops","oops-in-java","oops-in-python"],"created_at":"2024-11-08T02:49:09.188Z","updated_at":"2026-02-23T15:31:30.726Z","avatar_url":"https://github.com/lana-20.png","language":null,"readme":"# \u003cimg src=\"https://user-images.githubusercontent.com/70295997/217401037-a4b98acb-c52c-49b4-90ed-a44b27407ea2.png\" width=40\u003e Encapsulation\n\n_Encapsulation is one of the core concepts of Object Oriented Programming (OOP)._ Mainly, encapsulation binds together the code and data in a single unit of work (a class) and acts as a defensive shield that doesn’t allow the external code to access this data directly. _It is the technique of hiding the object state from the outer world and exposing a set of \u003ccode\u003epublic\u003c/code\u003e methods for accessing this state. When each object keeps its state \u003ccode\u003eprivate\u003c/code\u003e inside a class, encapsulation is achieved. This is why encapsulation is also known as the **data-hiding** mechanism._\n\nThe code that takes advantage of encapsulation is:\n- [x] loosely coupled (eg., I can change the names of the class variables without breaking the client code)\n- [x] reusable\n- [x] secure (the client is not aware of how data is manipulated inside the class)\n- [x] easy to test (it’s easier to test methods than fields)\n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810749-64a94f9b-00ad-4d5b-b112-2baa6157bb52.png\" width=40\u003e In Java, encapsulation can be achieved via the _access modifiers/specifiers_, \u003ccode\u003epublic\u003c/code\u003e, \u003ccode\u003eprivate\u003c/code\u003e, and \u003ccode\u003eprotected\u003c/code\u003e.\n\nEverything is \u003ccode\u003epublic\u003c/code\u003e by default. \n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810799-021871c1-780a-484d-8634-690968fe9c05.png\" width=40\u003e [In Python](https://github.com/lana-20/encapsulation-python):\n* To make fields/variables and methods \u003ccode\u003eprotected\u003c/code\u003e, prefix them with a single underscore '_'. Eg., \u003ccode\u003e_field\u003c/code\u003e or \u003ccode\u003edef _method()\u003c/code\u003e. \n* To make fields/variables and methods \u003ccode\u003eprivate\u003c/code\u003e, prefix them with a dunder '__'. Eg., \u003ccode\u003e__field\u003c/code\u003e or \u003ccode\u003edef __method()\u003c/code\u003e.\n\nCommonly, when on object manages its own state, its state is declared via \u003ccode\u003eprivate\u003c/code\u003e variables and is accessed and/or modified via \u003ccode\u003epublic\u003c/code\u003e methods. For example, a \u003ccode\u003eCat\u003c/code\u003e class can have its own state represented by fields, such as \u003ccode\u003emood\u003c/code\u003e, \u003ccode\u003ehungry\u003c/code\u003e, and \u003ccode\u003eenergy\u003c/code\u003e. While the code external to the \u003ccode\u003eCat\u003c/code\u003e class cannot modify any of these fields directly, it can call \u003ccode\u003epublic\u003c/code\u003e methods, such as \u003ccode\u003eplay()\u003c/code\u003e, \u003ccode\u003efeed()\u003c/code\u003e and \u003ccode\u003esleep()\u003c/code\u003e that modify the \u003ccode\u003eCat\u003c/code\u003e state internally. The \u003ccode\u003eCat\u003c/code\u003e class may also have \u003ccode\u003eprivate\u003c/code\u003e methods that are not accessible outside the class, such as \u003ccode\u003emeow()\u003c/code\u003e. \n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/217401305-9cb67ac3-355c-443c-9318-0e5b9d3d64b1.png\" width=40\u003e This is encapsulation.\n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810749-64a94f9b-00ad-4d5b-b112-2baa6157bb52.png\" width=40\u003e [Java code sample](https://github.com/lana-20/oop-encapsulation/blob/main/java-class-cat):\n\n    public class Cat {\n\n      private int mood = 50;\n      private int hungry = 50;\n      private int energy = 50;\n\n      public void sleep() {\n        System.out.println(“Sleep …”);\n        energy ++;\n        hungry ++;\n      }\n\n      public void play() {\n        System.out.println(“Play …”);\n        mood ++;\n        energy --;\n        meow();\n      }\n\n      public void feed() {\n        System.out.println(“Feed …”);\n        hungry --;\n        mood ++;\n        meow();\n      }\n\n      private void meow() {\n        System.out.println(“Meow …”);\n      }\n\n      public int getMood() {\n        return mood;\n      }\n\n      public int getHungry() {\n        return hungry;\n      }\n\n      public int getEnergy() {\n        return energy;\n      }\n    }\n\nThe only way to modify the state is via the public methods, play(), feed(), and sleep().\n\n    public static void main(String[] args) {\n\n      Cat cat = new Cat();\n\n      cat.feed();\n      cat.play();\n      cat.feed();\n      cat.sleep();\n\n      System.out.println(“Energy: ” + cat.getEnergy());\n      System.out.println(“Mood: ” + cat.getMood());\n      System.out.println(“Hungry: ” + cat.getHungry());\n    }\n\nThe output is as follows:\n\n    Feed …Meow!Play …Meow!Feed …Meow!Sleep …\n\n    Energy: 50\n    Mood: 53\n    Hungry: 49\n\n\u003cimg src=\"https://user-images.githubusercontent.com/70295997/216810799-021871c1-780a-484d-8634-690968fe9c05.png\" width=40\u003e Here's the Python equivalent of the Java code, using Python property decorators to demonstrate encapsulation in Object Oriented Programming:\n\n    class Cat:\n        def __init__(self):\n            self._mood = 50\n            self._hungry = 50\n            self._energy = 50\n\n        def sleep(self):\n            print(\"Sleep…\")\n            self._energy += 1\n            self._hungry += 1\n\n        def play(self):\n            print(\"Play…\")\n            self._mood += 1\n            self._energy -= 1\n            self._meow()\n\n        def feed(self):\n            print(\"Feed…\")\n            self._hungry -= 1\n            self._mood += 1\n            self._meow()\n\n        def _meow(self):\n            print(\"Meow…\")\n\n        @property\n        def mood(self):\n            return self._mood\n\n        @property\n        def hungry(self):\n            return self._hungry\n\n        @property\n        def energy(self):\n            return self._energy\n\n    if __name__ == \"__main__\":\n        cat = Cat()\n\n        cat.feed()\n        cat.play()\n        cat.feed()\n        cat.sleep()\n\n        print(\"Energy:\", cat.energy)\n        print(\"Mood:\", cat.mood)\n        print(\"Hungry:\", cat.hungry)\n\nThe output is as follows:\n\n    Feed…\n    Meow…\n    Play…\n    Meow…\n    Feed…\n    Meow…\n    Sleep…\n    Energy: 51\n    Mood: 52\n    Hungry: 52\n\nIn this implementation, the \u003ccode\u003emood\u003c/code\u003e, \u003ccode\u003ehungry\u003c/code\u003e, and \u003ccode\u003eenergy\u003c/code\u003e fields are declared as protected using the leading underscore notation. The \u003ccode\u003eget_mood()\u003c/code\u003e, \u003ccode\u003eget_hungry()\u003c/code\u003e, and \u003ccode\u003eget_energy()\u003c/code\u003e methods are replaced with Python property getters (declared using the @property decorator).\n\nThis way, the internal state of the \u003ccode\u003eCat\u003c/code\u003e object can be accessed using the properties (eg., \u003ccode\u003ecat.energy\u003c/code\u003e), but the underlying fields cannot be directly modified.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flana-20%2Foop-encapsulation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flana-20%2Foop-encapsulation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flana-20%2Foop-encapsulation/lists"}