tag:blogger.com,1999:blog-694855878384792308.post5494011715700430930..comments2024-03-21T06:26:49.387-07:00Comments on Java67: 5 Rules of Method Overloading and Overriding in Java? Examplesjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-694855878384792308.post-68722200410863975462021-11-15T23:05:14.192-08:002021-11-15T23:05:14.192-08:00Mighty Impressive Article.Mighty Impressive Article.Pratush Mishrahttps://www.blogger.com/profile/04873304527283182638noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-88789520216332506552019-09-01T15:57:11.807-07:002019-09-01T15:57:11.807-07:00very good article.Neat and clear. Also good job ev...very good article.Neat and clear. Also good job everyone in the comments section to improvise it. We all keep learning together :)Anonymoushttps://www.blogger.com/profile/17962775916499265947noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-27343122847280610742018-09-25T04:21:47.621-07:002018-09-25T04:21:47.621-07:00Hello Madhu, it's very simple, in your case yo...Hello Madhu, it's very simple, in your case you have only two method with same name, overloaded whoAmI() and whoAmI(String name, String i). Third method name is different so it doesn't come into picture "whomAmI(String i, String name)".<br /><br />Since you are calling with two String arguments, second method is chosen over first one. simple. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-53305057206013382002018-08-27T00:56:47.017-07:002018-08-27T00:56:47.017-07:00Please look into the below code.
class UNIX {
pr...Please look into the below code.<br /><br />class UNIX {<br /> protected final void whoAmI() {<br /> System.out.println("I am UNIX");<br /> }<br /> <br /> protected final void whoAmI(String name, String i) {<br /> System.out.println("I am " + name);<br /> }<br /> <br /> protected final void whomAmI(String i, String name) {<br /> System.out.println("Trial on Reversing the arguments without changing the datatypes");<br /> }<br /> <br /> <br />}<br /><br />public class MethodOverloading {<br /> public static void main(String args[]) {<br /> UNIX unix = new UNIX();<br /> unix.whoAmI("Sri","Madhu");<br /> }<br />}<br /><br />Here the output is I am Sri<br /><br />How the compiler is calling that particular method only? and why I'm not getting any error like unreachable code or something of that kind.<br /><br />Thanks In Advance.<br /><br />Regards,<br />Madhu.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-38893849547405611142018-06-23T09:35:39.127-07:002018-06-23T09:35:39.127-07:00Overriding method cannot throw checked Exception w...Overriding method cannot throw checked Exception which is higher in hierarchy, than checked Exception thrown by overridden method<br /><br />Could someone please explain the reason for this rule ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-67476376647900560112018-01-04T11:13:01.650-08:002018-01-04T11:13:01.650-08:00Good job sir..Good job sir..Mr. Aseet Kumar Padhihttps://www.blogger.com/profile/02894467988385360828noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-29021621105422201552016-06-30T05:27:18.556-07:002016-06-30T05:27:18.556-07:00Hello @Anonymous, this is because if a code is wri...Hello @Anonymous, this is because if a code is written considering superclass in mind and only handling checked exception thrown by it then it won't be able to catch the new higher exception thrown by subclass. Since checked exception has to be handled at compile time, allowing overriding method to throw higher exception will conflict with this rule.<br /><br />e.g.<br /><br />try{<br /> searchFile();<br /><br />}catch(FileNotFoundException f){<br />}<br /><br />if searchFile() is overridden and started throwing IOException then this code will break. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-3800671827992229762016-06-29T23:34:01.390-07:002016-06-29T23:34:01.390-07:00In #2 point, Overriding method cannot throw checke...In #2 point, Overriding method cannot throw checked Exception which is higher in hierarchy. Can you please tell me the reason for this?<br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-49923603624572253362016-05-01T09:30:30.582-07:002016-05-01T09:30:30.582-07:00In second paragraph it is mentioned that " bo...In second paragraph it is mentioned that " both overloading and overriding means creating method of same name in either same class or child class " but in rules for overriding 1st rule(" A method can only be overridden in sub class, not in same class")<br /><br />Could you please clarify which one is correct??Anonymoushttps://www.blogger.com/profile/16704205360310435952noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-8544330249720976982015-03-18T04:29:21.985-07:002015-03-18T04:29:21.985-07:00This is also correct, there is no restriction on t...This is also correct, there is no restriction on throwing unchecked exception on overriding method. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-74852970272606293822015-03-18T04:28:50.927-07:002015-03-18T04:28:50.927-07:00Indeed, that's true from Java 1.5 onward, know...Indeed, that's true from Java 1.5 onward, known as covarient method overriding. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-81520934144033150642015-03-17T00:31:52.071-07:002015-03-17T00:31:52.071-07:00Please add the rule of the unchecked exception as ...Please add the rule of the unchecked exception as well.<br /><br />Overidding method can throw any new unchecked exception irrespective of whether super class has thrown any exception or not.Pranavhttps://www.blogger.com/profile/13792011360345096523noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-63374852702524992172015-03-17T00:30:03.475-07:002015-03-17T00:30:03.475-07:00Please update the rule of the return type in metho...Please update the rule of the return type in method signature as<br /><br />Return type of the overridding method must be same as that of the super class or it can be a sub type of the super class return type.Pranavhttps://www.blogger.com/profile/13792011360345096523noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-85522710868171867432014-11-17T22:58:01.036-08:002014-11-17T22:58:01.036-08:00Really Nice article, with one error: Point#6 for O...Really Nice article, with one error: Point#6 for Overriding<br />Return type for Overriding : It can be changed as covariant type in Java.<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-12324521909106274602014-10-09T08:29:30.252-07:002014-10-09T08:29:30.252-07:00Hello @Anonymous, thanks for your suggestion, Upda...Hello @Anonymous, thanks for your suggestion, Updated article to include importance of return type while overriding method in Java as well. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-1417218807346988052014-10-09T06:23:59.646-07:002014-10-09T06:23:59.646-07:00Please also add rules of return type in method sin...Please also add rules of return type in method singnature.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-87564680530683126282014-08-11T02:49:20.469-07:002014-08-11T02:49:20.469-07:00nice article
nice article<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-54293464310520750512014-05-27T11:28:25.695-07:002014-05-27T11:28:25.695-07:00Impressive...Good workImpressive...Good workrahulhttps://www.blogger.com/profile/14877953840843896826noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-35881089526487777642014-04-14T10:27:41.977-07:002014-04-14T10:27:41.977-07:00plz upload clear information of method signatureplz upload clear information of method signatureAnonymousnoreply@blogger.com