Timeline for When is method overloading appropriate?
Current License: CC BY-SA 3.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 16, 2012 at 15:07 | comment | added | dukeofgaming | @blahman BTW and on another subject, when you have too many parameters and some of them are optional sometimes it is better to just send a single object or data structure that has all the default values, so you change the attributes of such object or data structure. That way you don't need to make 20 versions of the same method just to add a single parameter each time. | |
Feb 16, 2012 at 10:38 | comment | added | S.Robins | @blahman you should overload when you wish to use different types, a mixture of types, or even to add additional parameters. Overloading provides a way to create methods with default parameters where the param=value default syntax isn't supported. See my answer to see what I mean. | |
Feb 16, 2012 at 4:23 | comment | added | blahman |
If I understand your answer correctly, I should be overloading only when looking at the parameters will allow me to infer the differences in behaviour between, say, sum(String, String) and sum(int, int) ?
|
|
Feb 16, 2012 at 3:58 | review | Suggested edits | |||
Feb 16, 2012 at 4:19 | |||||
Feb 16, 2012 at 3:55 | comment | added | dukeofgaming | Derp, caffeine did get me this time =P, left the second part as the first and the first as the second for general interest. Thanks for the correction. | |
Feb 16, 2012 at 3:54 | history | edited | dukeofgaming | CC BY-SA 3.0 |
deleted 37 characters in body
|
Feb 16, 2012 at 3:52 | comment | added | S.Robins |
Your first example is an override. When you maintain a method's interface yet change the behaviour in a descendant. The implication is that you do not intend to offer an alternative method. Your second example however is correctly overloading. If your intention is to highlight when to override vs when to overload, you may wish to make this clearer in your answer, otherwise the whole when inheriting section is probably not needed. :)
|
|
Feb 16, 2012 at 3:32 | history | answered | dukeofgaming | CC BY-SA 3.0 |