Overloading Methods

Available

"Hello, Amigo! I'm going to tell you about method overloading."

"You can overload them too?! What a day!"

"You can do a lot with them, but we won't go into that right now."

"It's a deal."

"Overloading is a very simple operation. Actually, it's not even an operation on methods, though it is sometimes referred to by a frightening name: parametric polymorphism."

The thing you must remember is that every method in a class must have a unique name.

"Yeah, I know."

"Well, that's not entirely true. I mean, it's not at all true. A method does not have to have a unique name. What must be unique is the combination of a method name and the types of its parameters. This combination is also known as a method signature."

Code Comments
public void print();
public void print2();
This is allowed. The two methods have unique names.
public void print();
public void print(int n);
And this is also allowed. The two methods have unique names (signatures).
public void print(int n, int n2);
public void print(int n);
The method names are still unique here.
public int print(int a);
public void print(int n);
But this is not allowed. The methods are not unique, even though different types are being passed.
public int print(int a, long b);
public long print(long b, int a);
But this is allowed. The method parameters are unique.

"I've already seen that somewhere."

"Yep. When you type "System.out.println", IntelliJ IDEA suggests a couple dozen versions of print methods that use different parameters. The compiler simply identifies and then calls the required method based on the types of the parameters you pass."

"That's not so difficult. But it's not polymorphism."

"Or more accurately, it's not method overriding."

By the way, I want to point out that the parameter names are irrelevant. They are lost during compilation. Once a method is compiled, only its name and parameter types are known.

Comments (11)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Thomas
Level 13 , Scottsdale, United States
17 January, 05:49
These forums/comments ... obviously from very smart people ... are worth the Codegym $$$ price. If you go to 5-10 other web sources; sure you can find good info. I don't have time to watch YouTube too. Codegym should have UPVOTE on spectacular comments and give awards out. What must be unique is the combination of a method name and the types of its parameters. This combination is also known as a method "signature". As far as parametric polymorphism ... does barf translate ?
Chang You
Level 47 , Santa Rosa, United States
24 December 2020, 16:58
🥳
Karas Java Developer
16 November 2020, 01:47
Details, details, details...
Darko Jakimovski
Level 18 , Kriva Palanka, Macedonia, The Former Yugoslav Republic of
24 April 2019, 12:14
The fourth example's description is not accurate. The same type is being passed, but different types are being returned. Or am I missing something?
// Java Poser
Level 18 , Cincinnati, United States
24 April 2019, 12:34
I tried this and it said that the method has already been defined in the class. I assume it's because the JVM has no idea which return type to use when the method is called. The only way around this is to change the method names(ex: print1, and print2), which isn't overloading.
Darko Jakimovski
Level 18 , Kriva Palanka, Macedonia, The Former Yugoslav Republic of
24 April 2019, 12:39
It's legit that isn't overloading, but the description is kinda meh :/ because the same type is being passed, if you pass a different type no problemo. Nevermind, it's cool and the gang.
Blaise
Level 20 , London, United Kingdom
19 May 2020, 19:28
Order of parameters is different at the fourth one: int long vs long int. That's what counts. Return type is not part of method signature. "Method name, number of parameters, parameter type and order of parameters."
Sela
Level 20 , Poland
13 July 2020, 13:09
you're right is: "...different types are being passed" should be: "...different return types" types of passed arguments are the same i.e. int, only names of arguments differ i.e. a, n
// Java Poser
Level 18 , Cincinnati, United States
8 April 2019, 13:52
ahhh yess parametric polymorphism, one of my favorite instruments. mind if i play tune?
Darko Jakimovski
Level 18 , Kriva Palanka, Macedonia, The Former Yugoslav Republic of
24 April 2019, 12:14
I laughed so hard :D
// Java Poser
Level 18 , Cincinnati, United States
24 April 2019, 12:25
lmao!