{"id":466,"date":"2021-01-06T22:37:31","date_gmt":"2021-01-06T22:37:31","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=466"},"modified":"2021-01-06T22:47:52","modified_gmt":"2021-01-06T22:47:52","slug":"constructor-chaining-etc","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/constructor-chaining-etc\/","title":{"rendered":"Constructor Chaining, etc."},"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-6a3f8696288f3\" 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-6a3f8696288f3\"  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\/constructor-chaining-etc\/#Other_Calls_to_Superclass_Methods\" >Other Calls to Superclass Methods<\/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\/constructor-chaining-etc\/#Superclass_Method_Calls\" >Superclass Method Calls<\/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\/constructor-chaining-etc\/#Finalizer_Chaining\" >Finalizer Chaining<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">Within a <strong><em>subclass<\/em><\/strong> constructor, the syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>super( \/* arguments *\/ );<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">calls the appropriate constructor for that subclass&#8217;s <strong><em>superclass<\/em><\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>This syntax is&nbsp;<em>only valid within a subclass constructor<\/em><\/li><li>It must be&nbsp;<em>the first statement within the body of the constructor<\/em><\/li><li>It is analogous to the syntax for one constructor of a class to call another constructor of the same class:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><strong>this( \/* arguments *\/ );<\/strong><\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is called&nbsp;<em><strong>constructor chaining<\/strong><\/em>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the first statement in a constructor is not an explicit call to a superclass constructor with the&nbsp;<strong>super&nbsp;<\/strong>keyword, then Java implicitly inserts the call:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>super();<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, a call to the superclass&#8217;s default constructor.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>If the superclass does not have a default (zero-argument) constructor, it causes a compilation error.<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<ul class=\"wp-block-list\"><li>There is one exception to the rule about implicitly inserting&nbsp;<strong>super()<\/strong>:<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>If the first line of the subclass constructor uses the syntax:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>this( \/* arguments *\/ );<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">to call another constructor within the subclass, then Java relies on the second subclass constructor to invoke the superclass constructor (directly, or indirectly)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Other_Calls_to_Superclass_Methods\"><\/span>Other Calls to Superclass Methods<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Within any <em>subclass<\/em> method, the syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>super.<\/strong><em>method<\/em><strong>( \/* arguments *\/ );<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">calls the appropriate method for the <em>superclass<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Superclass_Method_Calls\"><\/span>Superclass Method Calls<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Within any subclass method, the syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>super.<\/strong><em>method<\/em><strong>( \/* arguments *\/ );<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">calls the appropriate method for the superclass.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We saw this in the <strong>Manager<\/strong> class&#8217;s <strong>raiseSalary<\/strong> method when it called the <strong>Employee<\/strong> class&#8217;s <strong>raiseSalary<\/strong> method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    public void raiseSalary(double byPercent)\n    {\n        \/\/ Add 1\/2% bonus for every year of service\n        Date today = new Date();\n        double bonus =\n                0.5 * (today.getYear() + 1900 - getHireYear());\n        <strong>super.raiseSalary(byPercent + bonus);<\/strong>\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><u>Question:<\/u><\/strong>&nbsp;What would be the result of omitting the&nbsp;<code><strong>super.<\/strong><\/code>&nbsp;prefix, as in:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    public void raiseSalary(double byPercent)\n    {\n        \/\/ Add 1\/2% bonus for every year of service\n        Date today = new Date();\n        double bonus =\n                0.5 * (today.getYear() + 1900 - getHireYear());\n        <strong>raiseSalary(byPercent + bonus);<\/strong>\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">???<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Finalizer_Chaining\"><\/span>Finalizer Chaining<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You might think that, because Java enforces constructor chaining, that it would enforce finalizer chaining &#8212; in other words, that a finalizer method for a subclass would automatically invoke the finalizer method for the superclass.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>Java does&nbsp;<u>not<\/u>&nbsp;do this. You must do this explicitly, if you write a subclass finalizer method, and if you want this behavior.<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Usually, the code will look as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>protected void finalize() throws Throwable\n{\n    \/* ... *\/\n    super.finalize();\t\/\/ Typically the last line of the method...\n}<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Again, do not think of a finalize method being equivalent to a destructor (as in C++), or something that can be used as some kind of Java &#8220;deconstructor&#8221;.&nbsp; In most cases, if you think along those lines, you will get into trouble!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Within a subclass constructor, the syntax: super( \/* arguments *\/ ); calls the appropriate constructor for that subclass&#8217;s superclass. This syntax is&nbsp;only valid within a subclass constructor It must be&nbsp;the first statement within the body of the constructor It is analogous to the syntax for one constructor of a class to call another constructor of [&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-466","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":"Within a subclass constructor, the syntax: super( \/* arguments *\/ ); calls the appropriate constructor for that subclass&#8217;s superclass. This syntax is&nbsp;only valid within a subclass constructor It must be&nbsp;the first statement within the body of the constructor It is analogous to the syntax for one constructor of a class to call another constructor of&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/466","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=466"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/466\/revisions"}],"predecessor-version":[{"id":471,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/466\/revisions\/471"}],"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=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}