{"id":10864,"date":"2015-06-07T03:50:11","date_gmt":"2015-06-07T03:50:11","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=10864"},"modified":"2015-06-07T03:50:11","modified_gmt":"2015-06-07T03:50:11","slug":"java-programming-overloaded-methods","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-programming-overloaded-methods\/","title":{"rendered":"Java programming:  overloaded methods"},"content":{"rendered":"<h1>The tutor explores the concept of overloaded methods in Java.<\/h1>\n<p>A class can have variables and methods.  Perhaps surprisingly, it can have more than one method with the same name.  Said methods will have different parameter lists.  The compiler will choose the method that makes sense based on the call.<\/p>\n<p>Consider, for example, this very simple class:<br \/>\n<span style=\"font-family:monospace;color:olive\"><br \/>\npublic class Messager{<br \/>\npublic void tellMessage(){<br \/>\nSystem.out.println(&#8220;My message to you is Cheers!&#8221;);<br \/>\n}<br \/>\npublic void tellMessage(String mess){<br \/>\nSystem.out.println(&#8220;My message to you is &#8220;+mess);<br \/>\n}<br \/>\n}<\/span><\/p>\n<p>Notice the two <span style=\"font-family:monospace\">tellMessage<\/span> methods:  the first expects no parameters, while the second expects a string.<\/p>\n<p>One could write a driver class, such as the following, to test the overloaded method in the class <span style=\"font-family:monospace\">Messager<\/span>:<br \/>\n<span style=\"font-family:monospace;color:olive\"><br \/>\nclass MessagerDriver{<br \/>\npublic static void main(String[] args){<br \/>\nMessager m = new Messager();<br \/>\nif (args.length>0){<br \/>\nm.tellMessage(args[0]);<br \/>\n}<br \/>\nelse{<br \/>\nm.tellMessage();<br \/>\n}<br \/>\n}\/\/end main<br \/>\n}\/\/end Driver<br \/>\n<\/span><br \/>\nAfter compiling, one could run <span style=\"font-family:monospace\">MessagerDriver<\/span> both with and without a command line argument, then notice the different versions of <span style=\"font-family:monospace\">tellMessage<\/span> being invoked.<\/p>\n<p>For those who aren&#8217;t yet comfortable with the nuts and bolts of compiling and running a Java program:  I hope to give some tips next post.<\/p>\n<p>HTH:)<\/p>\n<p>Source:<\/p>\n<p>Niemeyer, Patrick and Jonathan Knudsen.  <u>Learning Java<\/u>.  Sebastopol, CA:  O&#8217;Reilly &nbsp;&nbsp;Media, Inc., 2005.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor explores the concept of overloaded methods in Java. A class can have variables and methods. Perhaps surprisingly, it can have more than one method with the same name. Said methods will have different parameter lists. The compiler will &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-programming-overloaded-methods\/\"> <span class=\"screen-reader-text\">Java programming:  overloaded methods<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[602,861,860],"class_list":["post-10864","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-java-programming","tag-method-overloading","tag-overloaded-methods"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=10864"}],"version-history":[{"count":17,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10864\/revisions"}],"predecessor-version":[{"id":10881,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10864\/revisions\/10881"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=10864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=10864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=10864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}