{"id":2293,"date":"2017-05-09T15:25:38","date_gmt":"2017-05-09T15:25:38","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2293"},"modified":"2017-05-14T08:56:58","modified_gmt":"2017-05-14T08:56:58","slug":"single-dispatch-multiple-dispatch-dynamic-dispatch-double-dispatch","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/single-dispatch-multiple-dispatch-dynamic-dispatch-double-dispatch\/","title":{"rendered":"Single Dispatch, Multiple Dispatch, Dynamic Dispatch, Double Dispatch"},"content":{"rendered":"<p><strong>Single Dispatch<\/strong><\/p>\n<p><strong>SingleDispatch.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SingleDispatch \r\n{\r\n\tpublic void print()\r\n\t{\r\n\t  System.out.println(&quot;Single Dispatch&quot;);\t\r\n\t}\r\n\r\n \tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t  SingleDispatch objSingleDis = new SingleDispatch();\r\n \t  objSingleDis.print();\t\r\n        }\r\n}\r\n<\/pre>\n<p><strong>Dynamic Dispatch<\/strong><br \/>\nDynamic dispatch is the same thing which we do in strategy pattern.The actual method which is called is known at the runtime<\/p>\n<p><strong>Account.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Account \r\n{\r\n  public void calculateinterest();\r\n}\r\n<\/pre>\n<p><strong>SavingsAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SavingsAccount implements Account\r\n{\r\n\t@Override\r\n\tpublic void calculateinterest() {\r\n\t\tSystem.out.println(&quot;Intrest is 8%&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>LoanAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class LoanAccount implements Account\r\n{\r\n\t@Override\r\n\tpublic void calculateinterest() {\r\n\t\tSystem.out.println(&quot;Intrest is 11.5%&quot;);\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p><strong>CalculateInterest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CalculateInterest \r\n{\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t\tAccount objSavAcc = new SavingsAccount();\r\n\t\tAccount objLoanAcc = new LoanAccount();\r\n\t\t\r\n\t\tobjSavAcc.calculateinterest();\r\n\t\tobjLoanAcc.calculateinterest();\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>What is Multiple Dispatch<\/strong><br \/>\n<strong>Account.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Account \r\n{\t\r\n\tpublic void calculateinterest() {\r\n\t\tSystem.out.println(&quot;Intrest is 11.5%&quot;);\r\n\t}\r\n\t\r\n\tpublic void calculateinterest(int prePayment) {\r\n\t\tSystem.out.println(&quot;Intrest is 11.5% with prePayment&quot;);\r\n\t}\r\n\t\r\n\tpublic void calculateinterest(int prePayment, boolean floatingIntrest) {\r\n\t\tSystem.out.println(&quot;Intrest is 11.5% with floatingIntrest&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p>Now in the above example we have a Account class where the functions are overloaded.Now when the code gets executed then the methods are chosen based on the parameters passed. <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CalculateInterest \r\n{\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t\tAccount objAcc = new Account();\t\t\r\n\t\t\r\n\t\tobjAcc.calculateinterest();\r\n\t\tobjAcc.calculateinterest(23);\r\n\t\tobjAcc.calculateinterest(23, true);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nIntrest is 11.5%\r\nIntrest is 11.5% with prePayment\r\nIntrest is 11.5% with floatingIntrest\r\n<\/pre>\n<p>Now Java doesn&#8217;t supports multiple dispatch as above.The above code is an example of overloading.<\/p>\n<p><strong>Overloading vs Multiple Dispatch<\/strong><br \/>\nThe Difference between overloading and Multiple Dispatch is when the method to be called is decided at compile time then it is Overloading, if the method is decided at runtime then it is multiple dispatch<\/p>\n<p><strong>What is Double Dispatching?<\/strong><br \/>\nIn Double Dispatching the choosing of the method happens dynamically twice.In the below example the method is chosen similar to strategy pattern first time during call of <strong>viewReport method<\/strong> and again during choosing which <strong>printReport method<\/strong> to be called based on the class type its is called similar to  <\/p>\n<p><strong>Staff.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Staff \r\n{\r\n\tvoid viewReport(Report objReport);\r\n}\r\n<\/pre>\n<p><strong>Teacher.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Teacher implements Staff\r\n{\r\n\t@Override\r\n\tpublic void viewReport(Report objReport) \r\n\t{\r\n                System.out.println(&quot;View Report of Teacher&quot;);\r\n\t\tobjReport.printReport(this);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Principal.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Principal implements Staff\r\n{\r\n\t@Override\r\n\tpublic void viewReport(Report objReport) \r\n\t{\t\t\r\n                System.out.println(&quot;View Report of Principal&quot;);\r\n\t\tobjReport.printReport(this);\t\t\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Report.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Report \r\n{\r\n\tpublic void printReport(Teacher objTeacher)\r\n\t{\r\n\t\tSystem.out.println(&quot;Can print report of her class&quot;); \r\n\t}\r\n\t\r\n\tpublic void printReport(Principal objPrincipal)\r\n\t{\r\n\t\tSystem.out.println(&quot;Can print report of all the class&quot;);\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p><strong>ShowReport.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ShowReport \r\n{\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t\tPrincipal objPrincipal = new Principal();\r\n\t\tTeacher objTeacher   = new Teacher();\r\n\t\tobjPrincipal.viewReport(new Report());\r\n\t\tobjTeacher.viewReport(new Report());\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output.java<\/strong><\/p>\n<pre>\r\nView Report of Principal\r\nCan print report of all the class\r\nView Report of Teacher\r\nCan print report of her class\r\n<\/pre>\n<p><em>In statically typed languages, including Java, the biggest difference between dispatch and overloading is that overloading is based on the static type of parameters (i.e. the choice of which method is actually called is decided at compile-time), while dispatch is based on the dynamic types (i.e. the decision is made at runtime). (Such languages usually don&#8217;t support multiple dispatch.)<\/em><\/p>\n<p>Another Example of Double Dispatch is Serialization.In Serialization the class which is Serializable calls the methods with itself as an argument.In the below example the writeObject method dispatches the call back to the ObjectOutputStream thus making this a double dispatch. ObjectOutputStream delegates back MuxPrinter the responsibility of writing its state onto stream. By Doing this ObjectOutputStream has decoupled itself from our object objMuxPrt. <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MuxPrinter implements Serializable\r\n{\r\n\r\n}\r\n\r\nMuxPrinter objMuxPrt = new MuxPrinter();\r\nObjectOutputStream oos = new ObjectOutputStream();\r\noos.writeObject(objMuxPrt);  \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Single Dispatch SingleDispatch.java public class SingleDispatch { public void print() { System.out.println(&quot;Single Dispatch&quot;); } public static void main(String&#x5B;] args) { SingleDispatch objSingleDis = new SingleDispatch(); objSingleDis.print(); } } Dynamic Dispatch Dynamic dispatch is the same thing which we do in strategy pattern.The actual method which is called is known at the runtime Account.java public interface&hellip; <a href=\"https:\/\/codethataint.com\/blog\/single-dispatch-multiple-dispatch-dynamic-dispatch-double-dispatch\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142],"tags":[225],"class_list":["post-2293","post","type-post","status-publish","format-standard","hentry","category-java-concepts","tag-whatsapp-java-group"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2293"}],"version-history":[{"count":10,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions"}],"predecessor-version":[{"id":2315,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions\/2315"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}