{"id":10422,"date":"2014-11-01T10:00:31","date_gmt":"2014-11-01T10:00:31","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=10422"},"modified":"2020-09-01T07:18:33","modified_gmt":"2020-09-01T07:18:33","slug":"overloading-and-overriding","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/overloading-and-overriding\/","title":{"rendered":"Overloading and Overriding"},"content":{"rendered":"<p>\u2018Overloading\u2019 and \u2018Overriding\u2019 is yet another crucial concept which is part of the \u2018Java SE Programmer I\u2019 exam. This concept like the other concepts of core Java, are important from a developer\u2019s standpoint as well as for the certification exam. We will first discuss overloading and then move onto overriding.<\/p>\n<p><strong>Overloading:<br \/>\n<\/strong>\u2018Overloading\u2019 as the name suggests is done by creating multiple methods with same name \u2013 but with different argument lists, return type and access modifier. Out of the three points mentioned, it is important to note that a method is considered overloaded ONLY when it has a different argument list. Specifying a different return type and access modifier is optional.<\/p>\n<p>Consider the example specified below:<\/p>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/10\/Overloading-Ex1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-10423 alignnone\" alt=\"Overloading-Ex1\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/10\/Overloading-Ex1.jpg\" width=\"487\" height=\"561\" \/><\/a><\/p>\n<p><strong>\u00a0<\/strong>In the above example, we are overloading the method \u2018area\u2019 to find the area of different figures (rectangle, square, circle). All the three overloaded methods differ in their argument lists.<\/p>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/10\/Overloading-Ex2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-10424 alignnone\" alt=\"Overloading-Ex2\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/10\/Overloading-Ex2.jpg\" width=\"525\" height=\"399\" \/><\/a><\/p>\n<p style=\"padding-left: 30px\">Since, all methods feature different argument lists they are overloaded methods. In addition, they also have different return values. Overloading is by far the simpler of two concepts and these are the key takeaways for the exam:<\/p>\n<ol style=\"padding-left: 30px\">\n<li><span style=\"line-height: 1.5em\">Any number of overloaded methods can exist in the same class or sub-class<\/span><\/li>\n<li>The overloaded methods MUST differ in their argument lists.<\/li>\n<li>Overloaded methods are really different methods \u2013 they just have the same name but with different argument lists, return types and access modifiers.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><b style=\"line-height: 1.5em\">Overriding:<br \/>\n<\/b>We next discuss the concept of \u2018overriding\u2019. Overriding is used in sub-classes to change the functionality of a method for that particular class. The concept of \u2018overriding\u2019 is much more stringent than the concept of \u2018overloading\u2019. Overriding methods should NOT have different arguments lists or return types. Their access modifier CANNOT be more restrictive and they cannot throw any new checked exceptions.<br \/>\nWe will illustrate this by an example:<\/p>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/10\/Overriding-Ex1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-10426 alignnone\" alt=\"Overriding-Ex1\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2014\/10\/Overriding-Ex1.jpg\" width=\"509\" height=\"318\" \/><\/a><\/p>\n<p><b style=\"line-height: 1.5em\">\u00a0<\/b>This will be the output of the above example.<\/p>\n<pre>This method specifies car travel\nThis method specifies the different forms of travel<\/pre>\n<p>Here, \u2018public void travel()\u2019 in the \u2018overrride\u2019 class is the overridden method. \u00a0The appropriate output is generated when the \u2018travel()\u2019 method is called on the super class as well as the sub-class.<\/p>\n<p>To conclude, here are a few points regarding overriding:<\/p>\n<ol>\n<li>If the argument list changes in a method we are trying to override, then it unintentionally becomes an \u2018overloaded method\u2019.<\/li>\n<li>Methods that are not inherited cannot be overridden.<\/li>\n<li>Methods which are marked static and final cannot be overridden.<\/li>\n<li>In addition to this, the crucial points stated earlier hold as well(argument lists, return types not changing, access modifier not being more restrictive and so on)<\/li>\n<\/ol>\n<p>We discussed overloading and overriding in this post. We will discuss some more fascinating features of the Java programming language relating to the certifications in the next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2018Overloading\u2019 and \u2018Overriding\u2019 is yet another crucial concept which is part of the \u2018Java SE Programmer I\u2019 exam. This concept like the other concepts of core Java, are important from a developer\u2019s standpoint as well as for the certification exam. We will first discuss overloading and then move onto overriding. Overloading: \u2018Overloading\u2019 as the name suggests is done by creating multiple methods with same name \u2013 but with different argument lists, return type and access modifier. Out of the three points mentioned, it is important to note that a method is considered overloaded ONLY when it has a different argument [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[967,1172,1173],"class_list":["post-10422","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-3","tag-overloading","tag-overriding"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":1,"uagb_excerpt":"\u2018Overloading\u2019 and \u2018Overriding\u2019 is yet another crucial concept which is part of the \u2018Java SE Programmer I\u2019 exam. This concept like the other concepts of core Java, are important from a developer\u2019s standpoint as well as for the certification exam. We will first discuss overloading and then move onto overriding. Overloading: \u2018Overloading\u2019 as the name&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/10422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=10422"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/10422\/revisions"}],"predecessor-version":[{"id":75998,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/10422\/revisions\/75998"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=10422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=10422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=10422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}