Rules Of Method Overloading As Well As Overriding Inwards Coffee
The overloading in addition to overriding dominion inwards JavaSince you lot tin seat notice either overload or override methods inwards Java, its of import to know what are the rules of overloading in addition to overriding inwards Java. whatever overloaded method or overridden method must follow rules of method overloading in addition to method overriding to avoid compile fourth dimension fault in addition to logical runtime errors; where you lot intend to override a method but method acquire overloaded. That is non uncommon in addition to occur several times when Java programmer tries to override equals inwards Java or overriding compareTo method inwards Java spell implementing Comparable interface, which accepts Object type of argument. From Java v onwards which introduces @Override annotation along alongside Enum, Generics in addition to varargs method you lot tin seat notice completely avoid that problem.
Anyway, let's meet rules of method overloading in addition to dominion of method overriding inwards Java. By the way, for those who are completely novel inwards programming, both overloading in addition to overriding way creating method of same get upwards inwards either same degree or youngster class.
In the instance of overriding, the master copy method inwards the raise degree is known every bit overridden method, spell novel method inwards the youngster degree is called the overriding method.
Anyway, let's meet rules of method overloading in addition to dominion of method overriding inwards Java. By the way, for those who are completely novel inwards programming, both overloading in addition to overriding way creating method of same get upwards inwards either same degree or youngster class.
In the instance of overriding, the master copy method inwards the raise degree is known every bit overridden method, spell novel method inwards the youngster degree is called the overriding method.
Rules of overloading a method inwards Java
Here is the listing of dominion which needs to live followed to overload a method inwards Java :
1) First in addition to initiatory off dominion to overload a method inwards Java is to alter method signature. method signature is made of number of arguments, type of arguments in addition to club of arguments if they are of unlike types. You tin seat notice alter whatever of these or combinations of them to overload a method inwards Java. For example, inwards next code, nosotros receive got changed number of arguments to overload whoAmI() method inwards Java :
By the way, right way to overload a method is to alter the number of declaration or type of declaration because it denotes that it offering same functionality but input is different. For illustration println() method of PrintStream degree has several overloaded version to convey unlike information types e.g. String, int, long, float, double, boolean etc. Similarly, EnumSet.of() method is overloaded to convey one, 2 or to a greater extent than values. The 3rd way to overload method yesteryear changing the club of declaration or type of declaration creates lot of confusion in addition to best to live avoided, every bit discussed inwards my postal service close overloading best practices inwards Java.
2) Return type of method is non share of method signature, thence simply changing the provide type volition non overload a method inwards Java. In fact, simply changing the provide type volition number inwards compile fourth dimension fault every bit "duplicate method X inwards type Y. Here is a screenshot of that fault inwards Eclipse :
See what is method overloading inwards Java for code illustration of these rules,
1) Influenza A virus subtype H5N1 method tin seat notice solely live overridden inwards sub class, non inwards same class. If you lot endeavour to do 2 methods alongside same signature inwards 1 degree compiler volition complain close it proverb "duplicate method inwards type Class", every bit shown inwards next screenshot :
2) Overriding method cannot throw checked Exception which is higher inwards hierarchy, than checked Exception thrown yesteryear overridden method. For illustration if overridden method throws IOException or ClassNotfoundException, which are checked Exception, than overriding method tin seat notice non throw java.lang.Exception because it comes higher inwards type hierarchy (it's super degree of IOException in addition to ClassNotFoundExcepiton). If you lot do so, compiler volition grab you lot every bit seen inwards next icon :
class UNIX { protected lastly void whoAmI() { System.out.println("I am UNIX"); } protected lastly void whoAmI(String name){ System.out.println("I am " + name); } }
By the way, right way to overload a method is to alter the number of declaration or type of declaration because it denotes that it offering same functionality but input is different. For illustration println() method of PrintStream degree has several overloaded version to convey unlike information types e.g. String, int, long, float, double, boolean etc. Similarly, EnumSet.of() method is overloaded to convey one, 2 or to a greater extent than values. The 3rd way to overload method yesteryear changing the club of declaration or type of declaration creates lot of confusion in addition to best to live avoided, every bit discussed inwards my postal service close overloading best practices inwards Java.
2) Return type of method is non share of method signature, thence simply changing the provide type volition non overload a method inwards Java. In fact, simply changing the provide type volition number inwards compile fourth dimension fault every bit "duplicate method X inwards type Y. Here is a screenshot of that fault inwards Eclipse :
See what is method overloading inwards Java for code illustration of these rules,
Method Overriding Rules inwards Java
Overriding is completely unlike than overloading in addition to thence it's rules are likewise different. For terminology, master copy method is known every bit overridden method in addition to novel method is known every bit overriding method. Following rules must live followed to correctly override a method inwards Java :1) Influenza A virus subtype H5N1 method tin seat notice solely live overridden inwards sub class, non inwards same class. If you lot endeavour to do 2 methods alongside same signature inwards 1 degree compiler volition complain close it proverb "duplicate method inwards type Class", every bit shown inwards next screenshot :
2) Overriding method cannot throw checked Exception which is higher inwards hierarchy, than checked Exception thrown yesteryear overridden method. For illustration if overridden method throws IOException or ClassNotfoundException, which are checked Exception, than overriding method tin seat notice non throw java.lang.Exception because it comes higher inwards type hierarchy (it's super degree of IOException in addition to ClassNotFoundExcepiton). If you lot do so, compiler volition grab you lot every bit seen inwards next icon :
3) Overriding method tin seat notice non cut access of overridden method. It way if overridden method is defined every bit populace than overriding method tin seat notice non live protected or packet private. Similarly if master copy method is protected in addition to thence overriding method cannot live package-private. You tin seat notice meet what happens if you lot violate this dominion inwards Java, every bit seen inwards this screenshot it volition throw compile fourth dimension fault proverb "You cannot cut visibility of inherited method of a class".
4) Overriding method tin seat notice increment access of overridden method. This is reverse of before rule, according to this if overridden method is declared every bit protected than overriding method tin seat notice live protected or public. Here is an illustration to meet that it's allowed inwards Java :
5) private, static in addition to final method tin seat notice non live overridden inwards Java. See other articles inwards this weblog to larn why you lot cannot override private, static or lastly method inwards Java. By the way, you lot tin seat notice cover someone in addition to static method but trying to override lastly method volition number inwards compile fourth dimension fault "Cannot override the lastly method from a class" every bit shown inwards below screenshot :
6) Return type of overriding method must live same every bit overridden method. Trying to alter provide type of method inwards youngster degree volition throw compile fourth dimension fault "return type is incompatible alongside raise degree method" every bit shown inwards next screenshot.
That’s all on Rules of method overloading in addition to overriding inwards Java, Its real of import to recall these dominion to correctly overload in addition to override whatever method inwards Java. Also recall to usage @Override annotation to accidentally overloading a method, instead of overriding it.
Further Learning
Top 10 tough gist Java interview inquiry in addition to answer
What is autoboxing inwards coffee alongside example
What is Serialization inwards Java
What is PATH in addition to Classpath inwards Java
10 Spring FAQ asked inwards interviews
0 Response to "Rules Of Method Overloading As Well As Overriding Inwards Coffee"
Post a Comment