{"id":16763,"date":"2016-11-14T08:53:53","date_gmt":"2016-11-14T08:53:53","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=16763"},"modified":"2022-06-03T05:54:30","modified_gmt":"2022-06-03T11:24:30","slug":"ocajp-encapsulation","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/ocajp-encapsulation\/","title":{"rendered":"OCAJP &#8211; HOW TO Apply encapsulation principles to a class?"},"content":{"rendered":"<p>This post is about the OCAJP exam objective <strong>&#8220;Apply encapsulation principles to a class&#8221;<\/strong>. In exam, You will asked like &#8220;How to make this class strongly encapsulated?&#8221;. In this article, we would explain encapsulation concept according to OCAJP exam and answer all your queries related to the <span>encapsulation concepts<\/span>.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/ocajp8-free-test\/\"><strong>OCAJP 8 Free Practice Questions<\/strong><\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/11\/ocajp-encapusulation.png\"><img decoding=\"async\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/11\/ocajp-encapusulation.png\" alt=\"OCAJP Encapsulation\" width=\"747\" height=\"350\" class=\"aligncenter size-full wp-image-16765\" \/><\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ea7e02;color:#ea7e02\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ea7e02;color:#ea7e02\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-encapsulation\/#What_is_Encapsulation\" >What is Encapsulation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-encapsulation\/#Why_we_need_Encapsulation\" >Why we need Encapsulation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-encapsulation\/#How_to_implement_Encapsulation\" >How to implement Encapsulation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-encapsulation\/#Encapsulation_benefits\" >Encapsulation benefits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-encapsulation\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Encapsulation\"><\/span>What is Encapsulation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2022\/06\/387.png\" alt=\"Image result for java encapsulation\" class=\" aligncenter\" \/><\/p>\n<ul>\n<li>Encapsulation is a process of binding the data (variables) and code acting on the data (methods) together as a single unit.<\/li>\n<li>Consider the example When you login into your Gmail or Yahoo mail account, there will be lot of back end process on which you have no control over it.<\/li>\n<li>So your password may be retrieved in an encrypted form, verified and only then you are given access. You do not have any control over how the password is verified and this keeps it safe from misuse.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/11\/what-is-encapsulation.png\"><img decoding=\"async\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/11\/what-is-encapsulation.png\" alt=\"what is encapsulation\" width=\"743\" height=\"138\" class=\"aligncenter size-full wp-image-16766\" \/><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Why_we_need_Encapsulation\"><\/span>Why we need Encapsulation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let us consider the example to understand encapsulation purpose.<\/p>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>class Student {\r\n\tint id;\r\n\tString name;\r\n\r\n}\r\n\r\npublic class Change {\r\n\r\n\tpublic static void main(String[] ar) {\r\n\r\n\t\tStudent s = new Student();\r\n\t\ts.id = -1;\r\n\t\ts.name=\"\";\r\n\t\ts.name=null;\r\n\t}\r\n}\r\n<\/pre>\n<ul>\n<li>Look at Student class. It contains two instance variables with default as access modifier.<\/li>\n<li>So any class within same package of that class can assign and change values to those variables by creating object for that class.<\/li>\n<li>By this , We don&#8217;t have control over What values are going to store in Student class variables.<\/li>\n<li>We can solve this problem by applying encapsulation to the Student class.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"How_to_implement_Encapsulation\"><\/span>How to implement Encapsulation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>The Java Bean class is the example of fully encapsulated class.<\/li>\n<li>The below are the rules to achieve encapsulation in Java<\/li>\n<li>Declare the variables of a class as private.<\/li>\n<\/ul>\n<ul>\n<li>\n<ul><\/ul>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>private int id;\r\nprivate String name;\r\n<\/pre>\n<ol>\n<li>Provide public setter methods to assign or change the variables values. The setter method name must have a prefix of &#8220;set&#8221; followed by the first letter of the property in uppercase followed by the remaining variable name.It should have &#8220;public&#8221; access modifier and &#8220;void&#8221; return type.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public void setId(int id) {\r\n\t\tthis.id = id;\r\n\t}\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\r\n<\/pre>\n<ol>\n<li>Provide public getter methods to get the variables values. The getter method name must have a prefix of &#8220;get&#8221;(if variable is not boolean) or &#8220;is&#8221;(if variable is boolean) followed by the first letter of the property in uppercase followed by the remaining variable name.It should have &#8220;public&#8221; access modifier and type of that variable as return type.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public int getId() {\r\n\t\treturn id;\r\n\t}\r\n\r\npublic String getName() {\r\n\t\treturn name;\r\n\t}\r\npublic boolean isAllowed(){\r\n return true;\r\n}\r\n<\/pre>\n<\/li>\n<li>Below is the complete program.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>class Student {\r\n\tprivate int id;\r\n\tprivate String name;\r\n\r\n\tpublic int getId() {\r\n\t\treturn id;\r\n\t}\r\n\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\r\n\tpublic void setName(String name) {\r\n\t\tif (!name.equals(\"\") &amp;&amp; !name.equals(null))\r\n\t\t\tthis.name = name;\r\n\t}\r\n\r\n\tpublic void setId(int id) {\r\n\t\tif (id &gt; 0)\r\n\t\t\tthis.id = id;\r\n\t}\r\n\r\n}\r\n\r\npublic class Change {\r\n\r\n\tpublic static void main(String[] ar) {\r\n\r\n\t\tStudent s = new Student();\r\n\t\ts.setId(1);\r\n\t\ts.setName(\"OCA\");\r\n\t\tSystem.out.println(s.getId() + \"  \" + s.getName());\r\n\r\n\t}\r\n}\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Encapsulation_benefits\"><\/span>Encapsulation benefits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>A class can have total control over what is stored in its fields.<\/li>\n<li>The fields of a class can be made read-only or write-only.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/\"><strong>600+ Practice Questions for OCAJP Certification Exam<\/strong><\/a><\/li>\n<\/ul>\n<p>We hope this article has provided good amount of details about the encapsulation and why it is necessary in the Java language. This objective will be tested in the OCAJP exam by asking the various rules that are either true or false. For the OCAJP exam remember Java Bean class rules and naming conventions.<\/p>\n<p>If you have any questions on OCAJP exam, please contact our support for help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about the OCAJP exam objective &#8220;Apply encapsulation principles to a class&#8221;. In exam, You will asked like &#8220;How to make this class strongly encapsulated?&#8221;. In this article, we would explain encapsulation concept according to OCAJP exam and answer all your queries related to the encapsulation concepts. OCAJP 8 Free Practice Questions What is Encapsulation Encapsulation is a process of binding the data (variables) and code acting on the data (methods) together as a single unit. Consider the example When you login into your Gmail or Yahoo mail account, there will be lot of back end process on [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[1140],"class_list":["post-16763","post","type-post","status-publish","format-standard","hentry","category-java","tag-ocajp-8-preparation"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":13,"uagb_excerpt":"This post is about the OCAJP exam objective &#8220;Apply encapsulation principles to a class&#8221;. In exam, You will asked like &#8220;How to make this class strongly encapsulated?&#8221;. In this article, we would explain encapsulation concept according to OCAJP exam and answer all your queries related to the encapsulation concepts. OCAJP 8 Free Practice Questions What&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=16763"}],"version-history":[{"count":3,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16763\/revisions"}],"predecessor-version":[{"id":82556,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16763\/revisions\/82556"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=16763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=16763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=16763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}