I've found resources that say method overloading is the ability for a language to use the same method with a different outcome, depending on context. Somehow, when I read other definitions, I fell like that's not the whole definition. Is there more to method overloading?
System Classes : Object Class : Duplicate Method. Duplicate Method. The Duplicate method makes a duplicate of the object, placing a reference to the new object in a reference variable. This method has the following syntax: refvariable = Object.Duplicate()
Method overloading is a form of polymorphism in OOP. Polymorphism allows objects or methods to act in different ways, according to the means in which they are used. One such manner in which the methods behave according to their argument types and number of arguments is method overloading.
In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { ... }
That's just a very general way of describing it. Method overloading allows you to use a single method name, but "overload it" (provide more than one version) depending on "context" (which is typically the type or number of arguments passed in). Since each method is separate, they can cause a "different outcome".
For example, using C#, you can write:
void Foo() // Version with no arguments
{
}
void Foo(int arg) // Version with a single int
{
}
void Foo(string arg1, double arg2) // Version with string and double parameters
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With