KwickAcademy Java · 6 min · free
Method Overloading
Overloading rules, how the compiler picks a version, ambiguous calls, how overriding differs, and two exam programs.
Follows the syllabus of: ICSE Class 10 Computer Applications, ISC Class 12 Computer Science (868)
On screen in this lesson
What is overloading?
| Same method name, in the same class |
| Parameter lists must differ |
| Differ in number, type or order of parameters |
| Return type alone is not enough |
| Also called compile time polymorphism |
Valid or not?
| Version 1 | Version 2 | Verdict |
|---|---|---|
| sum(int a) | sum(int a, int b) | Valid: number |
| sum(int a) | sum(double a) | Valid: type |
| f(int a, double b) | f(double a, int b) | Valid: order |
| int sum(int a) | void sum(int a) | Error: return |
| sum(int a) | sum(int b) | Error: names |
How the compiler picks
| 1. Exact match of argument types |
| 2. Widening: int to long, float or double |
| 3. Boxing: int to its object form Integer |
| No match, or two equal choices: compile error |
What is overriding?
| Inheritance: a child class gets a parent's members |
| Child rewrites a parent method |
| Same name AND same parameter list |
| Java picks the version while the program runs |
Overloading vs overriding
| Point | Overloading | Overriding |
|---|---|---|
| Classes | same class | parent and child |
| Parameters | must differ | must be same |
| Decided at | compile time | run time |
| Inheritance | not needed | needed |
| Polymorphism | compile time | run time |
Exam tips
| Write each version with a clear parameter list |
| Call every version at least once in main |
| Predict output by matching argument types first |
| Never overload by return type alone |
Quick answers
Can you overload by changing only the return type?
No. That is a compile error, because the parameter lists must differ.
Why does calc(5) run the long version and not the double one?
Java picks the closest widening, and long is closer to int than double.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Can two methods have the same name?42 sec
Which version runs for calc(5) when only long and double exist?42 sec
What is the main difference?41 sec
How do you find cube and cuboid volume with one name?36 secThe full lesson, in text
Hello students, welcome to Kwickprep. Can two methods in one class have exactly the same name? In Java, yes they can. Today we will learn method overloading, how Java picks the right version, how it differs from overriding, and two exam programs.
Method overloading means writing several methods with the same name. They are all in the same class. But their parameter lists must be different. They can differ in the number of parameters, their types, or their order. Changing only the return type is not enough, and gives a compile error. Polymorphism means one name with many forms, and overloading is compile time polymorphism.
Here are two methods named area. The first takes one side, for a square. The second takes length and width, for a rectangle. The call area of four has one argument. So Java runs the square version, and prints sixteen.
Let us test some pairs, a favourite exam question. One int against two ints is valid, because the number differs. Int against double is valid, because the type differs. Int then double, against double then int, is valid, because the order differs. Only a different return type is an error. Only different parameter names is also an error, because names do not count.
The compiler is the tool that turns your code into a program, and it chooses the version before the program runs. First, it looks for an exact match of the argument types. If there is none, it tries widening, which means moving to a bigger type, like int to long or double. Next, it tries boxing, which wraps an int into an Integer object. If nothing fits, or two versions fit equally well, you get a compile error.
Pause and predict. There is no calc that takes an int. The call is calc of five, and five is an int. Which version runs? Both long and double can hold an int. Java picks the closest widening, which is long. So it prints long.
Now a trap. Here the call is calc of two and three, and both are ints. The first version needs only the second value widened. The second version needs only the first value widened. Neither is better, so the compiler reports that the reference to calc is ambiguous.
Students often mix up overloading with overriding. Inheritance means a child class receives the members of a parent class. Overriding happens when the child class writes its own version of a parent method. The name and the parameter list must be exactly the same. Java decides which version to run while the program is running, based on the actual object.
Here Pet is the parent class, and Dog extends Pet, so Dog is the child. Both have a method called sound with no parameters. The variable pet is of type Pet, but the object is a Dog. At run time Java uses the Dog version. So it prints Woof, not Hmm.
Here is the comparison exams ask for. Overloading happens in the same class, while overriding needs a parent and a child class. In overloading the parameters must differ, but in overriding they must be the same. Overloading is decided at compile time, and overriding at run time. So inheritance is not needed for overloading, but it is needed for overriding. That is why they are called compile time and run time polymorphism.
Now two exam style programs. First, overload vol, short for volume. The version with one side finds the volume of a cube, side times side times side. The version with three values finds the volume of a cuboid, length times breadth times height. Vol of three prints twenty seven. Vol of two, three and four prints twenty four.
Second, a program where the argument type decides. One show takes an int, and the other takes a char, a single character. Pause and predict the output first. A in single quotes is a char, so it matches the char version exactly. It prints char A. Sixty five is an int, so it prints int sixty five. Java never widens when an exact match exists.
Here are four tips for overloading questions. Write each version with a clear, different parameter list. In main, call every version at least once, so the examiner sees each one work. When predicting output, match the argument types first, then think about widening. And never try to overload by changing only the return type.
Let us revise what we learned today. Overloading means the same name with different parameter lists. The compiler tries an exact match, then widening, then boxing. If two versions fit equally, the call is ambiguous. Overriding means a child class rewrites a method with the same signature. Overloading is decided at compile time, and overriding at run time.
Courses that teach this
| Course | Unit |
|---|---|
| ICSE Class 10 Computer Applications | Constructors |
| ISC Class 12 Computer Science (868) | Inheritance, Interfaces and Polymorphism |
| Programming All levels Java | Classes, Objects and Methods |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

