{"id":108,"date":"2026-03-02T08:24:45","date_gmt":"2026-03-02T03:24:45","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=108"},"modified":"2026-03-15T17:14:30","modified_gmt":"2026-03-15T12:14:30","slug":"encapsulation","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/","title":{"rendered":"Encapsulation"},"content":{"rendered":"\n<p>Encapsulation is one of the core principles of Object-Oriented Programming (OOP).<\/p>\n\n\n\n<p>Encapsulation means <strong>binding data (variables) and methods (functions) together inside a class<\/strong> and restricting direct access to some of the object&#8217;s components.<\/p>\n\n\n\n<p>In simple words:<br>Encapsulation = Data Hiding + Data Protection<\/p>\n\n\n\n<p>It helps protect data from accidental modification.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Encapsulation is Important<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Protects sensitive data<\/li>\n\n\n\n<li>Improves security<\/li>\n\n\n\n<li>Controls how data is accessed<\/li>\n\n\n\n<li>Makes code more organized<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example Without Encapsulation<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, name, marks):<br>        self.name = name<br>        self.marks = markss1 = Student(\"Hira\", 85)<br>s1.marks = -50   # Invalid value<br>print(s1.marks)<\/pre>\n\n\n\n<p>Here, anyone can change <code>marks<\/code> to an invalid value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Encapsulation Using Private Variables<\/strong><\/h2>\n\n\n\n<p>In Python, we make variables private by adding double underscore <code>__<\/code> before the variable name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, name, marks):<br>        self.name = name<br>        self.__marks = marks   # Private variable    def get_marks(self):<br>        return self.__marks    def set_marks(self, value):<br>        if value &gt;= 0:<br>            self.__marks = value<br>        else:<br>            print(\"Invalid marks\")s1 = Student(\"Hira\", 85)print(s1.get_marks())s1.set_marks(90)<br>print(s1.get_marks())s1.set_marks(-10)   # Invalid<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Output<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">85<br>90<br>Invalid marks<\/pre>\n\n\n\n<p>Now:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__marks<\/code> cannot be accessed directly<\/li>\n\n\n\n<li>It must be accessed using methods<\/li>\n\n\n\n<li>Validation is possible<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Accessing Private Variable Directly (Not Recommended)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">print(s1._Student__marks)<\/pre>\n\n\n\n<p>This works because Python uses name mangling, but it should not be used in practice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Access Specifiers in Python<\/strong><\/h2>\n\n\n\n<p>Python does not have strict access modifiers like other languages, but it follows naming conventions:<\/p>\n\n\n\n<p>Public Variable<br><code>self.name<\/code><br>Can be accessed anywhere<\/p>\n\n\n\n<p>Protected Variable<br><code>self._name<\/code><br>Should not be accessed outside class (convention only)<\/p>\n\n\n\n<p>Private Variable<br><code>self.__name<\/code><br>Cannot be accessed directly<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Benefits of Encapsulation<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Better control over data<\/li>\n\n\n\n<li>Prevents accidental changes<\/li>\n\n\n\n<li>Improves code maintainability<\/li>\n\n\n\n<li>Makes debugging easier<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Takeaway<\/strong><\/h2>\n\n\n\n<p>Encapsulation protects object data by restricting direct access and allowing controlled access through methods.<\/p>\n\n\n\n<p>It ensures safe, secure, and structured programming in OOP.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Concepts > Encapsulation<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773576813668\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":51,"template":"","class_list":["post-108","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Encapsulation - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn encapsulation in Python OOP, data hiding, private variables, getters, setters, and how to protect object data in classes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Encapsulation - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn encapsulation in Python OOP, data hiding, private variables, getters, setters, and how to protect object data in classes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T12:14:30+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/encapsulation\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/encapsulation\\\/\",\"name\":\"Encapsulation - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:24:45+00:00\",\"dateModified\":\"2026-03-15T12:14:30+00:00\",\"description\":\"Learn encapsulation in Python OOP, data hiding, private variables, getters, setters, and how to protect object data in classes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/encapsulation\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/encapsulation\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/encapsulation\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Concepts > Encapsulation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Encapsulation - One Language. Endless Possibilities","description":"Learn encapsulation in Python OOP, data hiding, private variables, getters, setters, and how to protect object data in classes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/","og_locale":"en_US","og_type":"article","og_title":"Encapsulation - One Language. Endless Possibilities","og_description":"Learn encapsulation in Python OOP, data hiding, private variables, getters, setters, and how to protect object data in classes.","og_url":"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-15T12:14:30+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/","url":"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/","name":"Encapsulation - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:24:45+00:00","dateModified":"2026-03-15T12:14:30+00:00","description":"Learn encapsulation in Python OOP, data hiding, private variables, getters, setters, and how to protect object data in classes.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/encapsulation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/encapsulation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Concepts > Encapsulation"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}