{"id":494,"date":"2021-01-07T16:56:48","date_gmt":"2021-01-07T16:56:48","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=494"},"modified":"2021-01-07T17:13:05","modified_gmt":"2021-01-07T17:13:05","slug":"data-hiding-encapsulation","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/data-hiding-encapsulation\/","title":{"rendered":"Data Hiding &#038; Encapsulation"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a6d49b9d2df7\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" 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: #999;color:#999\" 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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a6d49b9d2df7\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/data-hiding-encapsulation\/#Hide_Your_Class_Implementation\" >Hide Your Class Implementation!<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/data-hiding-encapsulation\/#The_Levels_of_Interface_for_a_Class\" >The Levels of Interface for a Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/data-hiding-encapsulation\/#How_to_Hide_Your_Class_Implementation\" >How to Hide Your Class Implementation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/data-hiding-encapsulation\/#Common_mistakes\" >Common mistakes<\/a><\/li><\/ul><\/nav><\/div>\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Hide_Your_Class_Implementation\"><\/span>Hide Your Class Implementation!<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the most powerful and important features of languages like Java is that it allows you to hide the implementation of your class. This is called&nbsp;<em><strong>data hiding<\/strong><\/em>, or&nbsp;<em><strong>encapsulation<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why would you want to do this?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because, by allowing the clients of your class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>knowledge of the class&nbsp;<em><strong>interface<\/strong><\/em><\/li><li>and&nbsp;<strong><em><u>no<\/u><\/em>&nbsp;<\/strong>knowledge of the class&nbsp;<em><strong>implementation<\/strong><\/em><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">you can&nbsp;<em>insulate those clients from any changes you make<\/em>&nbsp;in the class implementation.&nbsp;<em><strong>And, in any non-trivial class, you&nbsp;<u>will<\/u>&nbsp;make changes!<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another reason is&nbsp;<em>to protect you and your clients from accidental or willful misuse<\/em>. Typically, the implementation relies on complex interrelationships among data and methods. It is not reasonable to expect that all clients, given some knowledge of the implementation, will use that knowledge wisely.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Levels_of_Interface_for_a_Class\"><\/span>The Levels of Interface for a Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every class can be viewed from a number of perspectives (interfaces):<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>From inside the class itself (<em><strong>implementation<\/strong><\/em>)<\/li><li>From classes within the class&#8217;s package (<strong><em>mostly implementation<\/em><\/strong>)<\/li><li>From the outside world (<strong><em>public interface<\/em><\/strong>)<\/li><li>From subclasses of that class (<strong><em>inheritance interface<\/em><\/strong>)<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em><\/strong>&nbsp;even if there are no subclasses today, there may well be in the future!<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em>&nbsp;When you design a class, you should think about which parts of that class should be part of each of these interfaces.<\/strong><\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Hide_Your_Class_Implementation\"><\/span>How to Hide Your Class Implementation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You should make a habit of designing your classes using a <strong><em>very conservative model<\/em><\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Make&nbsp;<em>all<\/em>&nbsp;of your class attributes (data)&nbsp;<strong>private<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>class MyClass\n{\n    \/* ... *\/\n    private String m_name;\n};<\/strong><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>For each class\u00a0<em>attribute<\/em>\u00a0that your clients need to access:<ul><li>For each attribute that needs\u00a0<em><strong>read<\/strong>\u00a0<\/em>access, provide a\u00a0<strong>public\u00a0<em>accessor method<\/em><\/strong>:<br><code><strong>class MyClass <\/strong><br><strong>{    <\/strong><br>     <strong>\/* ... *\/ <\/strong><br>     <strong>public String getName() { return m_name; } <\/strong><br>     <strong>private String m_name; <br>};<\/strong><\/code><br><\/li><li>For each attribute that needs\u00a0<em><strong>write<\/strong>\u00a0<\/em>access, provide a\u00a0<strong>public\u00a0<em>mutator method<\/em><\/strong>:<br><code><strong>class MyClass <\/strong><br><strong>{ <\/strong><br>     <strong>\/* ... *\/ <\/strong><br>     <strong>public void setName(String name){ m_name = name; } <\/strong><br>     <strong>private String m_name; <br>};<\/strong><\/code><br><\/li><li>Choose the naming convention:<ul><li><strong><em>Type<\/em>\u00a0getFoo()<\/strong><\/li><li><strong>void setFoo(<em>Type<\/em>\u00a0value)<\/strong>where:<\/li><\/ul><ul><li><strong>foo<\/strong>\u00a0is the name of the attribute<\/li><li><em><strong>Type<\/strong><\/em>\u00a0is the datatype of that attribute<\/li><\/ul><\/li><\/ul><\/li><li>For each class\u00a0<em>method<\/em>:<ul><li>If the method is part of the\u00a0<em><strong>public interface<\/strong><\/em><ul><li>Make sure it\u00a0<em><strong>really is<\/strong>\u00a0part of the public interface<\/em>!<\/li><li>If it is,\u00a0<strong>make it<\/strong>\u00a0<strong>public<\/strong><\/li><li>If it&#8217;s not,\u00a0<em><strong>do not make it<\/strong><\/em>\u00a0<strong>public<\/strong>!<\/li><\/ul><\/li><li>A method that is part of the class&#8217;s\u00a0<strong><em>implementation<\/em><\/strong><ul><li>If the method is used by\u00a0<em>other classes in the same package<\/em><ul><li>Give it <em>package<\/em> visibility (no visibility modifiers)<\/li><li>Note that this amounts to it being part of the\u00a0<strong><em>package interface<\/em><\/strong><\/li><\/ul><\/li><li>If the method is used\u00a0<em>only by this class<\/em><ul><li>Make it\u00a0<strong>private<\/strong><\/li><\/ul><\/li><\/ul><\/li><li>A method that is part of the class&#8217;s\u00a0<strong><em>inheritance interface<\/em><\/strong><ul><li>Make it\u00a0<strong>protected<\/strong><\/li><li>Make sure that you provide the necessary attribute accessor\/mutator methods with the\u00a0<strong>protected\u00a0<\/strong>attribute (<em>don&#8217;t<\/em>\u00a0make an attribute itself\u00a0<strong>protected<\/strong>!)<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Common_mistakes\"><\/span>Common mistakes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Not making all class attributes (data)&nbsp;<strong>private<\/strong><\/li><li>Making too many methods&nbsp;<strong>public<\/strong><\/li><li>Making a class attribute (data)&nbsp;<strong>protected<\/strong>&nbsp;instead of creating&nbsp;<strong>protected<\/strong>&nbsp;accessor\/mutator methods<\/li><li>Making a method&nbsp;<strong>public<\/strong>, when it should be made&nbsp;<strong>protected<\/strong><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hide Your Class Implementation! One of the most powerful and important features of languages like Java is that it allows you to hide the implementation of your class. This is called&nbsp;data hiding, or&nbsp;encapsulation. Why would you want to do this? Because, by allowing the clients of your class: knowledge of the class&nbsp;interface and&nbsp;no&nbsp;knowledge of the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":61,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_front_end_style_editor":"no","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-494","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Hide Your Class Implementation! One of the most powerful and important features of languages like Java is that it allows you to hide the implementation of your class. This is called&nbsp;data hiding, or&nbsp;encapsulation. Why would you want to do this? Because, by allowing the clients of your class: knowledge of the class&nbsp;interface and&nbsp;no&nbsp;knowledge of the&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=494"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/494\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/494\/revisions\/499"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/61"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}