{"id":1037,"date":"2023-05-21T05:23:38","date_gmt":"2023-05-21T05:23:38","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.com\/?p=1037"},"modified":"2023-05-21T05:23:38","modified_gmt":"2023-05-21T05:23:38","slug":"what-is-encapsulation","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2023\/05\/21\/what-is-encapsulation\/","title":{"rendered":"What is Encapsulation?"},"content":{"rendered":"\n<p><strong>Purpose of Encapsulation:<\/strong><\/p>\n\n\n\n<ul>\n<li>Protects the code from others.<\/li>\n\n\n\n<li>Code maintainability.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>We are declaring \u2018a\u2019 as an integer variable and it should not be negative.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>public class Addition(){<\/code><br><code>int a = 5;<\/code><br><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>If someone changes the exact variable as \u201c<strong><em>a = -5\u201d&nbsp;<\/em><\/strong>then it is bad.<\/p>\n\n\n\n<p><strong>In order to overcome the problem we need to follow the steps below:<\/strong><\/p>\n\n\n\n<ul>\n<li>We can make the variable private or protected.<\/li>\n\n\n\n<li>Use public accessor methods such as set&lt;property&gt; and get&lt;property&gt;.<\/li>\n<\/ul>\n\n\n\n<p><strong>So that the above code can be modified as:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>publicclassAddition(){<\/code><br><code>private int a = 5; \/\/Here the variable is marked as private<\/code><br><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>The code below shows the getter and setter.<\/strong><\/p>\n\n\n\n<p>Conditions can be provided while setting the variable.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>get A(){<\/code><br><code>}<\/code><br><code>set A(int a){<\/code><br><code>if(a&amp;gt;0){<\/code><br><code>\/\/ Here condition is applied.........<\/code><br><code>}<\/code><br><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For encapsulation, we need to make all the instance variables private and create setter and getter for those variables. Which in turn will force others to call the setters rather than access the data directly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Purpose of Encapsulation: Example: We are declaring \u2018a\u2019 as an integer variable and it should&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1037"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1037\/revisions"}],"predecessor-version":[{"id":1038,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/1037\/revisions\/1038"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=1037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=1037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}