{"id":385,"date":"2021-01-04T17:06:28","date_gmt":"2021-01-04T17:06:28","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=385"},"modified":"2021-01-04T17:13:07","modified_gmt":"2021-01-04T17:13:07","slug":"methods-overloading","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/methods-overloading\/","title":{"rendered":"Methods &#038; Overloading"},"content":{"rendered":"\n<p>Consider a modified version of the class&nbsp;<strong>Circle<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [6,7,8,11,12,13]; title: ; quick-code: false; notranslate\" title=\"\">\nimport java.lang.Math;\n\npublic class Circle\n{\n    \/\/ Accessor methods\n    public double x()      { return m_x; }\n    public double y()      { return m_y; }\n    public double radius() { return m_r; }\n\n    \/\/ Mutator methods\n    public void x(double v)      { m_x = v; }\n    public void y(double v)      { m_y = v; }\n    public void radius(double r) { m_r = r; }\n\n    \/\/ Operations\/Attributes\n    public double getCircumference()\n                { return 2 * Math.PI * m_r; }\n    public double getArea()\n                { return Math.PI * m_r*m_r; }\n\n    \/\/ Data\n    double m_x, m_y; \/\/ coordinates of center\n    double m_r; \/\/ radius\n}\n<\/pre><\/div>\n\n\n<p>where I&#8217;ve changed the accessor and mutator method names for illustrative purposes.<\/p>\n\n\n\n<p>We now have two methods named <strong>x<\/strong>, two methods named <strong>y<\/strong>, and two methods named <strong>radius<\/strong>. Note that they have different <em>signatures<\/em> &#8212; that is, they have different sets of arguments.<\/p>\n\n\n\n<p>Creating multiple methods with the same name, but with different signatures, is known as <em>method overloading<\/em>.<\/p>\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>\u00a0The Java standard naming convention is to use\u00a0<code><strong>getFoo<\/strong><\/code>\u00a0and\u00a0<code><strong>setFoo<\/strong><\/code>\u00a0as names for the accessor and mutator methods for an attribute named\u00a0<code><strong>foo<\/strong><\/code>.\u00a0 I strongly recommend that you follow this convention in all your Java code.\u00a0 (Note that the Sun Java programmers did not initially follow this convention in early Java classes, but they do in all current code.)<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Consider a modified version of the class&nbsp;Circle: where I&#8217;ve changed the accessor and mutator method names for illustrative purposes. We now have two methods named x, two methods named y, and two methods named radius. Note that they have different signatures &#8212; that is, they have different sets of arguments. Creating multiple methods with the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":57,"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-385","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":"Consider a modified version of the class&nbsp;Circle: where I&#8217;ve changed the accessor and mutator method names for illustrative purposes. We now have two methods named x, two methods named y, and two methods named radius. Note that they have different signatures &#8212; that is, they have different sets of arguments. Creating multiple methods with the&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/385","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=385"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/385\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/385\/revisions\/391"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/57"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}