Saturday, November 9, 2019

Rules Of Method Overloading In Addition To Overriding Inward Coffee

The overloading together with overriding dominion inward JavaSince you lot tin grade notice either overload or override methods inward Java, its of import to know what are the rules of overloading together with overriding inward Java. whatsoever overloaded method or overridden method must follow rules of method overloading together with method overriding to avoid compile fourth dimension fault together with logical runtime errors;  where you lot intend to override a method but method acquire overloaded. That is non uncommon together with occur several times when Java programmer tries to override equals inward Java or  overriding compareTo method inward Java spell implementing Comparable interface, which accepts Object type of argument. From Java five onwards which introduces @Override annotation along alongside Enum, Generics together with varargs method you lot tin grade notice completely avoid that problem.


Anyway, let's meet rules of method overloading together with dominion of method overriding inward Java. By the way, for those who are completely novel inward programming, both overloading together with overriding agency creating method of same refer inward either same flat or pocket-size fry class.

In the instance of overriding, the master copy method inward the bring upwards flat is known equally overridden method, spell novel method inward the pocket-size fry flat is called the overriding method.



Rules of overloading a method inward Java

Here is the listing of dominion which needs to live followed to overload a method inward Java :



1) First together with firstly dominion to overload a method inward Java is to alter method signature. method signature is made of discover of arguments, type of arguments together with gild of arguments if they are of dissimilar types. You tin grade notice alter whatsoever of these or combinations of them to overload a method inward Java. For example, inward next code, nosotros guide maintain changed discover of arguments to overload whoAmI() method inward Java :
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 discover of declaration or type of declaration because it denotes that it offering same functionality but input is different. For instance println() method of PrintStream flat has several overloaded version to convey dissimilar 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 past times changing the gild of declaration or type of declaration creates lot of confusion together with best to live avoided, equally discussed inward my postal service virtually overloading best practices inward Java.



The overloading together with overriding dominion inward Java Rules of Method Overloading together with Overriding inward Java 2) Return type of method is non component subdivision of method signature, therefore simply changing the provide type volition non overload a method inward Java.  In fact, simply changing the provide type volition resultant inward compile fourth dimension fault equally "duplicate method X inward type Y. Here is a screenshot of that fault inward Eclipse :


See what is method overloading inward Java for code instance of these rules,




Method Overriding Rules inward Java

Overriding is completely dissimilar than overloading together with therefore it's rules are equally good different. For terminology, master copy method is known equally overridden method together with novel method is known equally overriding method. Following rules must live followed to correctly override a method inward Java :

1) H5N1 method tin grade notice solely live overridden inward sub class, non inward same class. If you lot travail to do 2 methods alongside same signature inward ane flat compiler volition complain virtually it proverb "duplicate method inward type Class", equally shown inward next screenshot :



2) Overriding method cannot throw checked Exception which is higher inward hierarchy, than checked
Exception thrown past times overridden method. For instance if overridden method throws IOException or ClassNotfoundException, which are checked Exception, than overriding method tin grade notice non throw java.lang.Exception because it comes higher inward type hierarchy (it's super flat of IOException together with ClassNotFoundExcepiton). If you lot do so, compiler volition grab you lot equally seen inward next picture :
The overloading together with overriding dominion inward Java Rules of Method Overloading together with Overriding inward Java


3) Overriding method tin grade notice non cut access of overridden method. It agency if overridden method is defined equally populace than overriding method tin grade notice non live protected or parcel private. Similarly if master copy method is protected therefore overriding method cannot live package-private. You tin grade notice meet what happens if you lot violate this dominion inward Java, equally seen inward this screenshot it volition throw compile fourth dimension fault proverb "You cannot cut visibility of inherited method of a class".
The overloading together with overriding dominion inward Java Rules of Method Overloading together with Overriding inward Java


4) Overriding method tin grade notice increment access of overridden method. This is reverse of before rule, according to this if overridden method is declared equally protected than overriding method tin grade notice live protected or public. Here is an instance to meet that it's allowed inward Java :
The overloading together with overriding dominion inward Java Rules of Method Overloading together with Overriding inward Java


5) private, static together with final method tin grade notice non live overridden inward Java. See other articles inward this spider web log to larn why you lot cannot override private, static or lastly method inward Java. By the way, you lot tin grade notice enshroud soul together with static method but trying to override lastly method volition resultant inward compile fourth dimension fault "Cannot override the lastly method from a class" equally shown inward below screenshot :




6) Return type of overriding method must live same equally overridden method. Trying to alter provide type of method inward pocket-size fry flat volition throw compile fourth dimension fault "return type is incompatible alongside bring upwards flat method" equally shown inward next screenshot.
The overloading together with overriding dominion inward Java Rules of Method Overloading together with Overriding inward Java


That’s all on Rules of method overloading together with overriding inward Java, Its really of import to recollect these dominion to correctly overload together with override whatsoever method inward Java. Also recollect to utilisation @Override annotation to accidentally overloading a method, instead of overriding it.

Further Learning
Top 10 tough meat Java interview query together with answer
What is autoboxing inward coffee alongside example
What is Serialization inward Java
What is PATH together with Classpath inward Java
10 Spring FAQ asked inward interviews

No comments:

Post a Comment