{"id":500,"date":"2021-01-07T17:14:19","date_gmt":"2021-01-07T17:14:19","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=500"},"modified":"2021-01-07T17:20:14","modified_gmt":"2021-01-07T17:20:14","slug":"abstract-classes","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/abstract-classes\/","title":{"rendered":"Abstract Classes"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a21c3163c0ec\" 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-6a21c3163c0ec\"  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\/abstract-classes\/#Concrete_vs_Abstract_Classes\" >Concrete vs. Abstract Classes<\/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\/abstract-classes\/#Abstract_Methods\" >Abstract Methods<\/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\/abstract-classes\/#Abstract_Classes\" >Abstract Classes<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/abstract-classes\/#The_Methods_of_Abstract_Classes\" >The Methods of Abstract Classes<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Concrete_vs_Abstract_Classes\"><\/span>Concrete vs. Abstract Classes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As you learn to construct class libraries, you will discover pretty quickly that you can often &#8216;push&#8217; a number of common attributes and\/or methods up the inheritance hierarchy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the&nbsp;<strong>Employee&nbsp;<\/strong>class would typically be used to contain the attributes and methods that are common to all employees, such as name, employee id, telephone number, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, you might decide that\u00a0<strong>Employee\u00a0<\/strong>isn&#8217;t sufficiently concrete enough to represent a real employee. In practice, an employee usually has\u00a0<em>a specific role<\/em>\u00a0in an organization &#8212; an employee is always a <strong>Secretary<\/strong>, <strong>Programmer<\/strong>, <strong>Manager<\/strong>,\u00a0<strong>Janitor<\/strong>,\u00a0<strong>President<\/strong>,\u00a0<strong>CEO<\/strong>,\u00a0<strong>FinancialAnalyst\u00a0<\/strong>or whatever.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instantiating an\u00a0<strong>Employee\u00a0<\/strong>might not actually make sense; it&#8217;s not sufficiently\u00a0<em>concrete<\/em>\u00a0&#8212; it&#8217;s too\u00a0<strong><em>abstract<\/em><\/strong>; we would need to know what kind of <strong>Employee<\/strong> we&#8217;re talking about.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But the class&nbsp;<strong>Employee&nbsp;<\/strong>is still very useful! It allows you to distill out the essence of all employees &#8212; those attributes and behaviors that all (or perhaps most) employees share. When you do pull shared attributes and behaviors into a class such as this, it can make the design of the subclasses of that class much cleaner, and can allow you&nbsp;<em>to reuse code more effectively<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A class like this &#8212; one that you never expect to instantiate (create instances of) alone; one that is considered incomplete &#8212; is called an&nbsp;<em><strong>abstract class<\/strong><\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Abstract_Methods\"><\/span>Abstract Methods<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s imagine that we know that all employees will have a <strong>raiseSalary()<\/strong> method (else we wouldn&#8217;t keep those employees very long!). However, it may well be that each class of employee has his or her salary raised using a different algorithm. We don&#8217;t know what that algorithm is when we create the&nbsp;<strong>Employee&nbsp;<\/strong>class ,&nbsp;<em>but we know that every employee will have one<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can define the&nbsp;<strong>raiseSalary()<\/strong>&nbsp;method on the <strong>Employee<\/strong> class, but not actually implement it there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To do this, we have to declare the method&nbsp;<strong>abstract<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public abstract void raiseSalary(double byPercent);<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, an abstract method:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>uses the&nbsp;<strong>abstract&nbsp;<\/strong>keyword, and<\/li><li><strong><em>has a declaration, but no implementation<\/em>.<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Abstract_Classes\"><\/span>Abstract Classes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">However, a class with one or more abstract methods must itself be declared <strong>abstract<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public abstract class Employee\n{\n    public abstract void raiseSalary(double byPercent);\n    \/\/ ...\n}<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(The Java compiler will remind you if you forget!)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An abstract method promises that all non-abstract subclasses of this class will implement that abstract method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Abstract methods act as placeholders for methods to be implemented in the subclasses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A class contains abstract methods if any of the following is true:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It explicitly contains a declaration of an abstract method<\/li><li>It inherits an abstract method from its direct superclass, but does not implement that method.<\/li><li><em>&#8220;A direct superinterface of the class declares or inherits a method and the class neither declares it nor inherits a method that implements it. &#8220;<\/em><br>(See later)<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Only abstract classes may contain abstract methods. If a class contains abstract methods, but is&nbsp;<em>not<\/em>&nbsp;declared&nbsp;<strong>abstract<\/strong>, then a compile-time error occurs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Methods_of_Abstract_Classes\"><\/span>The Methods of Abstract Classes<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Abstract classes&nbsp;<em>do not have to contain only abstract methods<\/em>. They may contain a mixture of abstract and concrete methods &#8212; and in fact, usually do.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a good idea to push as much functionality as possible (as appropriate to that class) into the more abstract classes &#8212; it allows you to reuse code more easily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may declare a class to be abstract, even if it does not contain any abstract methods. This prevents the class from being instantiated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Concrete vs. Abstract Classes As you learn to construct class libraries, you will discover pretty quickly that you can often &#8216;push&#8217; a number of common attributes and\/or methods up the inheritance hierarchy. For example, the&nbsp;Employee&nbsp;class would typically be used to contain the attributes and methods that are common to all employees, such as name, employee [&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-500","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":"Concrete vs. Abstract Classes As you learn to construct class libraries, you will discover pretty quickly that you can often &#8216;push&#8217; a number of common attributes and\/or methods up the inheritance hierarchy. For example, the&nbsp;Employee&nbsp;class would typically be used to contain the attributes and methods that are common to all employees, such as name, employee&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/500","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=500"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/500\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/500\/revisions\/505"}],"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=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}