Questions tagged [overloading]

A type of polymorphism where different functions with the same name or different implementations of an operator are invoked based on the data types of the parameters passed. DO NOT USE FOR OVERLOADING IN PHP; THE MEANING OF THAT TERM IN PHP IS UNRELATED.

The terms overloading and overloaded may refer to:

Don't confuse it with

Related tags:

6857 questions
2098
votes
28 answers

Does Java support default parameter values?

I came across some Java code that had the following structure: public MyParameterizedFunction(String param1, int param2) { this(param1, param2, false); } public MyParameterizedFunction(String param1, int param2, boolean param3) { //use all…
gnavi
  • 25,122
  • 7
  • 21
  • 11
901
votes
37 answers

Function overloading in Javascript - Best practices

What is the best way(s) to fake function overloading in Javascript? I know it is not possible to overload functions in Javascript as in other languages. If I needed a function with two uses foo(x) and foo(x,y,z) which is the best / preferred…
hamdiakoguz
  • 15,795
  • 9
  • 33
  • 27
744
votes
15 answers

Optional Parameters in Go?

Can Go have optional parameters? Or can I just define two different functions with the same name and a different number of arguments?
devyn
  • 16,615
  • 6
  • 24
  • 14
591
votes
18 answers

Constructor overload in TypeScript

Has anybody done constructor overloading in TypeScript. On page 64 of the language specification (v 0.8), there are statements describing constructor overloads, but there wasn't any sample code given. I'm trying out a really basic class declaration…
Ted
  • 6,011
  • 2
  • 13
  • 4
411
votes
5 answers

TypeScript function overloading

Section 6.3 of the TypeScript language spec talks about function overloading and gives concrete examples on how to implement this. However if I try something like this: export class LayerFactory { constructor (public styleFactory:…
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
377
votes
21 answers

Polymorphism vs Overriding vs Overloading

In terms of Java, when someone asks: what is polymorphism? Would overloading or overriding be an acceptable answer? I think there is a bit more to it than that. IF you had a abstract base class that defined a method with no implementation, and…
Brian G
  • 53,704
  • 58
  • 125
  • 140
339
votes
18 answers

Can you overload controller methods in ASP.NET MVC?

I'm curious to see if you can overload controller methods in ASP.NET MVC. Whenever I try, I get the error below. The two methods accept different arguments. Is this something that cannot be done? The current request for action 'MyMethod' on…
Papa Burgundy
  • 6,397
  • 6
  • 42
  • 48
321
votes
20 answers

Python function overloading

I know that Python does not support method overloading, but I've run into a problem that I can't seem to solve in a nice Pythonic way. I am making a game where a character needs to shoot a variety of bullets, but how do I write different functions…
Bullets
  • 3,251
  • 3
  • 15
  • 6
303
votes
16 answers

How to achieve function overloading in C?

Is there any way to achieve function overloading in C? I am looking at simple functions to be overloaded like foo (int a) foo (char b) foo (float c , int d) I think there is no straight forward way; I'm looking for workarounds if any exist. …
FL4SOF
  • 4,161
  • 6
  • 28
  • 24
268
votes
14 answers

Function overloading by return type?

Why don't more mainstream statically typed languages support function/method overloading by return type? I can't think of any that do. It seems no less useful or reasonable than supporting overload by parameter type. How come it's so much less…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
237
votes
17 answers

How do I use method overloading in Python?

I am trying to implement method overloading in Python: class A: def stackoverflow(self): print ('first method') def stackoverflow(self, i): print ('second method', i) ob=A() ob.stackoverflow(2) but the output is second…
user1335578
  • 2,587
  • 2
  • 18
  • 15
236
votes
10 answers

Accessing constructor of an anonymous class

Lets say I have a concrete class Class1 and I am creating an anonymous class out of it. Object a = new Class1(){ void someNewMethod(){ } }; Now is there any way I could overload the constructor of this anonymous class. Like…
Saravanan M
  • 4,697
  • 5
  • 35
  • 37
232
votes
9 answers

PHP function overloading

Coming from C++ background ;) How can I overload PHP functions? One function definition if there are any arguments, and another if there are no arguments? Is it possible in PHP? Or should I use if else to check if there are any parameters passed…
Vamsi Krishna B
  • 11,377
  • 15
  • 68
  • 94
217
votes
8 answers

Properly removing an Integer from a List

Here's a nice pitfall I just encountered. Consider a list of integers: List list = new ArrayList(); list.add(5); list.add(6); list.add(7); list.add(1); Any educated guess on what happens when you execute list.remove(1)? What…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
167
votes
7 answers

Why does the Scala compiler disallow overloaded methods with default arguments?

While there might be valid cases where such method overloadings could become ambiguous, why does the compiler disallow code which is neither ambiguous at compile time nor at run time? Example: // This fails: def foo(a: String)(b: Int = 42) = a +…
soc
  • 27,983
  • 20
  • 111
  • 215
1
2 3
99 100