Glossary: Overload

What is overload?

・ Overloading, overload ・ Perform in the same class (?) ・ One of the techniques of object-oriented programming -Define multiple methods with the same name but different arguments and return values. -Define multiple functions (methods) with the same name and distinguish them by the difference in arguments.

-Define multiple methods with the same name with different argument types in advance. → You can make a choice when calling the method → Appropriate method can be called according to the argument / return type → You can reduce conditional branching inside the check method at the time of calling → Improved readability and maintainability

【merit】 ・ Prevention of method complexity -Prevention of mass description of similar and non-similar methods

【Demerit】 ・ If it increases too much, it may create a method that makes it difficult to understand the function.

"The essence of overload is It should be applied to the method or constructor in the case of "arguments, return values, individual behaviors are different, but the essence of behaviors is the same". Overloading the method in the case of "the behavior is the same except for the argument type" is likely to cause confusion. ”

That's right. It's a little difficult. If you want to rephrase it in your own words

Creating a lot of methods that "have the same behavior but different argument types" is not preferable because it causes confusion. It should be used when "arguments, return values, and behavior after calling are completely different, but the essence is the same". Targets are methods and constructors. A constructor is a method that is created and executed at the time of instantiation.

Wonder. Please point out any misrecognition.

As long as I hear the voices of the people around me who actually started coding (unfortunately I haven't put it in yet and have no plans), I can identify the "type" both conceptually and in terms of business content. It seems that it is difficult. I'm wondering if this is all practice and experience.

I haven't looked into overrides yet, so I'd like to write them down here or elsewhere.

Recommended Posts

Glossary: Overload
Overload
[Java] Overload
[Java] [Basic] Glossary