Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is method overloading? [duplicate]

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?

like image 474
kypalmer Avatar asked Mar 28 '13 15:03

kypalmer


People also ask

What is duplicate method in java?

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()

What is method overloading?

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.

What is method overloading example?

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() { ... }


1 Answers

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
{
}
like image 90
Reed Copsey Avatar answered Sep 30 '22 13:09

Reed Copsey