0
0
Javaprogramming~15 mins

Method overloading in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is method overloading in Java?
Method overloading is when a class has multiple methods with the same name but different parameters (different type, number, or order). It helps perform similar tasks in different ways.
touch_appClick to reveal answer
beginner
Can method overloading change only the return type of methods?
No. Method overloading requires different parameter lists. Changing only the return type is not enough for overloading.
touch_appClick to reveal answer
intermediate
How does Java decide which overloaded method to call?
Java looks at the method name and the arguments you pass. It matches the method with the closest parameter types to your arguments.
touch_appClick to reveal answer
beginner
Example: What will this code print?

public class Test {
  void show(int a) { System.out.println("int: " + a); }
  void show(String a) { System.out.println("String: " + a); }
  public static void main(String[] args) {
    Test t = new Test();
    t.show(5);
    t.show("Hi");
  }
}
Output: int: 5 String: Hi Explanation: The show method is called with an int and a String, so Java calls the matching overloaded method each time.
touch_appClick to reveal answer
beginner
Why is method overloading useful?
It lets you use the same method name for similar actions but with different inputs. This makes code easier to read and organize.
touch_appClick to reveal answer
Which of these is a valid method overloading example?
Avoid print(int a) and void print(String a)
Bint add(int a) and int add(int a)
Cvoid show() and int show()
Dvoid run() and void run()
Can two overloaded methods differ only by return type?
AYes, always
BYes, if parameters are the same
CNo, never
DOnly if methods are static
What happens if no matching overloaded method is found?
AJava calls a random method
BJava throws a compile-time error
CJava calls the first method declared
DJava calls the method with the closest return type
Which is NOT a way to overload a method?
AChange number of parameters
BChange parameter types
CChange parameter order
DChange method name
Why might a programmer use method overloading?
ATo reuse method names for similar tasks
BTo confuse other programmers
CTo reduce the number of classes
DTo avoid writing methods
Explain method overloading and how Java chooses which method to call.
Give an example of method overloading with at least two methods and explain the output when called.