{"id":61,"date":"2020-12-31T22:01:53","date_gmt":"2020-12-31T22:01:53","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=61"},"modified":"2021-01-06T20:17:21","modified_gmt":"2021-01-06T20:17:21","slug":"inheritance-polymorphism","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/","title":{"rendered":"Inheritance &#038; Polymorphism"},"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-6a6d46f7e18ce\" 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-6a6d46f7e18ce\"  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\/#What_is_Inheritance\" >What is Inheritance?<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/#Why_is_Inheritance_important\" >Why is Inheritance important?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/#Classifications_Taxonomies_and_Inheritance\" >Classifications, Taxonomies, and Inheritance<\/a><\/li><\/ul><\/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\/#Inheritance\" >Inheritance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/#Inheritance_vs_Containment\" >Inheritance vs. Containment<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/#Containment_Aggregation\" >Containment (Aggregation)<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/#Inheritance-2\" >Inheritance<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">The features that make a programming language truly &#8220;Object-Oriented&#8221; are usually considered to be&nbsp;<em><strong>inheritance<\/strong><\/em>&nbsp;and&nbsp;<em><strong>polymorphism<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here we&#8217;ll examine the concepts.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Inheritance\"><\/span>What is Inheritance?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_is_Inheritance_important\"><\/span>Why is Inheritance important?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Have you ever written some code, and then later found that you needed to write something that was almost, but not quite, the same?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What did you do?<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Cut and paste the code, and then change it to suit? (Yeah, I know&#8230;)<\/li><li>Modify the existing code so it could handle both cases?<\/li><li>Why isn&#8217;t either of those solutions a good idea?<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>Inheritance<\/strong><\/em>&nbsp;provides a mechanism to allow you to extend existing code in a natural way.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Classifications_Taxonomies_and_Inheritance\"><\/span>Classifications, Taxonomies, and Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Humans are always classifying things!<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>Classification<\/strong><\/em>&nbsp;is a way of extracting out the common attributes of several like objects, and leaving only the differences.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Classification enables us to deal with new things in terms of what is already familiar to us; reduces volume of information; helps simplify.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We often call such classifications&nbsp;<em><strong>taxonomies<\/strong><\/em>. Taxonomies are everywhere.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"605\" height=\"278\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/inhsub1.gif\" alt=\"\" class=\"wp-image-438\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:42px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some more:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"644\" height=\"614\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/inhsub4.gif\" alt=\"\" class=\"wp-image-439\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:47px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Inheritance\"><\/span>Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Inheritance<\/em> is an abstraction mechanism which allows classes to be related hierarchically.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Allows classes to share definitions<\/li><li>Makes commonality of code explicit.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, consider the problem of drawing different kinds of graphical shapes. We can extract the common attributes and behaviors of any kind of shape, and encapsulate it in a <strong>Shape<\/strong> class:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"446\" height=\"208\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/inhsub2.gif\" alt=\"\" class=\"wp-image-440\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:53px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Circle<\/strong>, <strong>Triangle<\/strong> and <strong>Square<\/strong> classes&nbsp;<em><strong>inherit<\/strong><\/em>&nbsp;(or&nbsp;<em><strong>derive<\/strong><\/em>) their behaviors and properties from class Shape.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Inheritance_vs_Containment\"><\/span>Inheritance vs. Containment<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Containment_Aggregation\"><\/span>Containment (Aggregation)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A class can&nbsp;<em><strong>contain<\/strong><\/em>&nbsp;another class (a&nbsp;<em><strong>has-a<\/strong><\/em>&nbsp;relationship):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/\/ Wheel.java\npublic class Wheel\n{\n    \/* Details... *\/\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [18]; title: ; quick-code: false; notranslate\" title=\"\">\n\/\/ Car.java\npublic class Car\n{\n    public final int WHEEL_COUNT = 4;\n\n    \/\/ Constructor\n    public Car(String make, int maxSpeed)\n    { \n\tm_make = make; m_maxSpeed = maxSpeed; \n\tm_wheels = new Wheel&#x5B;WHEEL_COUNT];\n\tfor (int i = 0; i &lt; WHEEL_COUNT; i++)\n\t    m_wheels&#x5B;i] = new Wheel();\n    }\n\n    \/\/ Data\n    private String m_make;\n    private int m_maxSpeed;\n    private Wheel m_wheels&#x5B;]; \/\/ a Car has wheels\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Inheritance-2\"><\/span>Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">&#8230;or a class can\u00a0<em><strong>inherit from<\/strong><\/em>\u00a0another class (an\u00a0<em><strong>is-a<\/strong><\/em>\u00a0or\u00a0<em><strong>is-a-kind-of<\/strong><\/em>\u00a0relationship):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/\/ WheeledVehicle.java\npublic class WheeledVehicle\n{\n    \/\/ Constructor\n    public WheeledVehicle(int wheelCount)\n    {\n\tm_wheelCount = wheelCount;\n\tm_wheels = new Wheel&#x5B;wheelCount];\n\tfor (int i = 0; i &lt; wheelCount; i++)\n\t    m_wheels&#x5B;i] = new Wheel();\n    }\n\n    \/\/ Data\n    private int m_wheelCount;\n    private Wheel m_wheels&#x5B;]; \n\t\/\/ a WheeledVehicle has wheels\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [2,3]; title: ; quick-code: false; notranslate\" title=\"\">\n\/\/ Truck.java\npublic class Truck extends WheeledVehicle\n\t\/\/ Truck is-a-kind-of WheeledVehicle\n{\n    \/\/ Constructor\n    public Truck(String make, int maxSpeed)\n    { \n\tsuper(WHEEL_COUNT);\n\tm_make = make; m_maxSpeed = maxSpeed; \n    }\n\n    \/\/ Data\n    private static int WHEEL_COUNT = 4;\n    private String m_make;\n    private int m_maxSpeed;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Whether to use&nbsp;<em><strong>inheritance<\/strong><\/em>&nbsp;or&nbsp;<em><strong>containment<\/strong><\/em>&nbsp;depends on what you\u2019re trying to do:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Assuming that you are trying to decide whether a class B should inherit from a class A, or contain a class A, you should ask yourself:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>&#8220;Is B&nbsp;<strong>a kind of<\/strong>&nbsp;A?&#8221;<\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">If so, then use\u00a0<em><strong>inheritance<\/strong><\/em>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>&#8220;Does B&nbsp;<strong>have an<\/strong>&nbsp;A?&#8221;<\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">If so, then use\u00a0<em><strong>containment<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes the answer is obvious:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>&#8220;Is a Car a kind of Vehicle?&#8221;<\/strong><\/em><\/p><p><em><strong>&#8220;Does a Car have an Engine?&#8221;<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">and sometimes it\u2019s not:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>&#8220;Is a Point a kind of Location?&#8221;<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>&#8220;Does a Point have a Location?&#8221;<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The features that make a programming language truly &#8220;Object-Oriented&#8221; are usually considered to be&nbsp;inheritance&nbsp;and&nbsp;polymorphism. Here we&#8217;ll examine the concepts.&nbsp; What is Inheritance? Why is Inheritance important? Have you ever written some code, and then later found that you needed to write something that was almost, but not quite, the same? What did you do? Cut [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":34,"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-61","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":"The features that make a programming language truly &#8220;Object-Oriented&#8221; are usually considered to be&nbsp;inheritance&nbsp;and&nbsp;polymorphism. Here we&#8217;ll examine the concepts.&nbsp; What is Inheritance? Why is Inheritance important? Have you ever written some code, and then later found that you needed to write something that was almost, but not quite, the same? What did you do? Cut&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/61","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=61"}],"version-history":[{"count":5,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/61\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/61\/revisions\/446"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/34"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}