{"id":472,"date":"2021-01-06T22:49:35","date_gmt":"2021-01-06T22:49:35","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=472"},"modified":"2021-01-07T16:27:27","modified_gmt":"2021-01-07T16:27:27","slug":"visibility-modifiers-inheritance","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/visibility-modifiers-inheritance\/","title":{"rendered":"Visibility Modifiers &#038; Inheritance"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 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-6a06f621f07f7\" 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-6a06f621f07f7\"  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\/visibility-modifiers-inheritance\/#Protected\" >Protected<\/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\/visibility-modifiers-inheritance\/#Visibility_Modifiers\" >Visibility Modifiers<\/a><\/li><\/ul><\/nav><\/div>\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Protected\"><\/span>Protected<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Earlier, we talked about the visibility modifiers&nbsp;<strong>public&nbsp;<\/strong>and&nbsp;<strong>private<\/strong>. Now, with inheritance, we can talk about the third visibility modifier keyword:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>protected<\/strong><\/p><\/blockquote>\n\n\n\n<p>Here&#8217;s an updated discussion, including this new&nbsp;<strong>protected<\/strong>&nbsp;modifier:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Visibility_Modifiers\"><\/span>Visibility Modifiers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The keywords&nbsp;<strong>public&nbsp;<\/strong>and&nbsp;<strong>private&nbsp;<\/strong>and&nbsp;<strong>protected&nbsp;<\/strong>are&nbsp;<em>visibility modifiers<\/em>, which have the following effect:<\/p>\n\n\n\n<p>A&nbsp;<strong>private&nbsp;<\/strong>member of a class&nbsp;<em>is visible only in methods defined within that class<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>public&nbsp;<\/strong>member of a class&nbsp;<em>is visible within the class where it is defined, and within all classes that are in the same package as that class<\/em>.<\/p>\n\n\n\n<p>If a member is declared with&nbsp;<em>no visibility modifiers<\/em>, then it has&nbsp;<em>default package visibility<\/em>, and&nbsp;<em>is visible&nbsp;<strong>only<\/strong>&nbsp;within the class that defines it and within classes defined in the same package<\/em>. (This is analogous to the&nbsp;<em>friend<\/em>&nbsp;concept in C++ &#8212; all classes within a package are &#8216;friendly&#8217; to each other.)<\/p>\n\n\n\n<p>A&nbsp;<strong>protected&nbsp;<\/strong>member of a class&nbsp;<em>is visible within the class where it is defined<\/em>, and&nbsp;<em>within all subclasses of that class<\/em>, and also&nbsp;<em>within all classes that are in the same package as that class<\/em>.<\/p>\n\n\n\n<p>To summarize:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th> <\/th><th>Member<\/th><th>Visibility<\/th><th> <\/th><th> <\/th><\/tr><tr><th>Accessible to:<\/th><th>public<\/th><th>protected<\/th><th><em>package<\/em><\/th><th>private<\/th><\/tr><tr><td>Same class<\/td><td>yes<\/td><td>yes<\/td><td>yes<\/td><td>yes<\/td><\/tr><tr><td>Class in same Package<\/td><td>yes<\/td><td>yes<\/td><td>yes<\/td><td>no<\/td><\/tr><tr><td>Subclass in different package<\/td><td>yes<\/td><td>yes<\/td><td>no<\/td><td>no<\/td><\/tr><tr><td>Non-subclass, different package<\/td><td>yes<\/td><td>no<\/td><td>no<\/td><td>no<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Here are some simple rules to help you choose which visibility modifiers to use, when:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use&nbsp;<strong>public&nbsp;<\/strong>only for methods and constants that form part of the&nbsp;<em>public interface<\/em>&nbsp;(sometimes called the API) of the class.<\/li><li>Use the&nbsp;<em>default package visibility<\/em>&nbsp;(a.k.a. &#8220;package private&#8221;) for fields and methods that you want to be&nbsp;<em>hidden from outside of the package<\/em>, but to which you want&nbsp;<em>cooperating (&#8216;friend&#8217;) classes within the package to have access<\/em>.<\/li><li>Use&nbsp;<strong>protected&nbsp;<\/strong>when you want to&nbsp;<em>hide fields or methods from code that uses your class, but want those fields and methods to be fully accessible to code that extends your class.<\/em>&nbsp;&nbsp;<\/li><li>Use&nbsp;<strong>private&nbsp;<\/strong>for fields and methods that are&nbsp;<em>only used inside the class and should be hidden from everywhere else<\/em>.&nbsp;<\/li><\/ul>\n\n\n\n<div class=\"wp-block-group has-white-background-color has-background\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p><em><strong>NOTE: It is very good practice to always make fields\/data<\/strong><\/em><strong><em> <\/em><\/strong><strong><em>members within a class to be\u00a0<\/em><\/strong><strong>private<em>.\u00a0 I maintain that it is essential for good class design.\u00a0 There is only one exception to this rule:\u00a0<\/em><\/strong><\/p>\n\n\n\n<p><strong><em>When a field is part of the appropriate interface (API) for the class.&nbsp; This should only be the case for static final fields (i.e. constants). Make such a static final field:<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>public<\/strong><\/code><em><strong>, if it is part of the class&#8217;s public interface<\/strong><\/em><\/li><li><strong><em>package private, if it is part of the class&#8217;s contribution to the package&#8217;s internal interface (i.e. used only by methods within that package)<\/em><\/strong><\/li><li><code><strong>protected<\/strong><\/code><em><strong>, if it is part of the class&#8217;s subclass interface (i.e. used only by methods within subclasses of that class)<\/strong><\/em><\/li><\/ul>\n\n\n\n<p><em><strong>My approach is to take the conservative approach:<\/strong><\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong>Start out making all fields explicitly<\/strong>&nbsp;<\/em><code><strong>private<\/strong><\/code><em>.<\/em><\/li><li><em><strong>Only change this decision, if you are absolutely convinced that the specified field is truly a part of your class&#8217;s official interfaces (APIs)<\/strong><\/em><\/li><\/ul>\n\n\n\n<p><em><strong>Sometimes this involves some iteration during design, where you realize that some fields or methods should become non-private (this is rare for fields, but happens more often for methods as the design evolves).<\/strong><\/em><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Protected Earlier, we talked about the visibility modifiers&nbsp;public&nbsp;and&nbsp;private. Now, with inheritance, we can talk about the third visibility modifier keyword: protected Here&#8217;s an updated discussion, including this new&nbsp;protected&nbsp;modifier: Visibility Modifiers The keywords&nbsp;public&nbsp;and&nbsp;private&nbsp;and&nbsp;protected&nbsp;are&nbsp;visibility modifiers, which have the following effect: A&nbsp;private&nbsp;member of a class&nbsp;is visible only in methods defined within that class. A&nbsp;public&nbsp;member of a class&nbsp;is visible [&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_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-472","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":"Protected Earlier, we talked about the visibility modifiers&nbsp;public&nbsp;and&nbsp;private. Now, with inheritance, we can talk about the third visibility modifier keyword: protected Here&#8217;s an updated discussion, including this new&nbsp;protected&nbsp;modifier: Visibility Modifiers The keywords&nbsp;public&nbsp;and&nbsp;private&nbsp;and&nbsp;protected&nbsp;are&nbsp;visibility modifiers, which have the following effect: A&nbsp;private&nbsp;member of a class&nbsp;is visible only in methods defined within that class. A&nbsp;public&nbsp;member of a class&nbsp;is visible&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/472","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=472"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/472\/revisions"}],"predecessor-version":[{"id":478,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/472\/revisions\/478"}],"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=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}