Method Overloading - Notes by Shariq SP

Understanding Method Overloading in Java

Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameter lists. These methods can perform similar tasks but accept different types or numbers of arguments.

Example 1: Method Overloading

In this example, we have a class Calculator with multiple add methods:


            public class Calculator {
                // Method to add two integers
                public int add(int num1, int num2) {
                    return num1 + num2;
                }
                
                // Method to add three integers
                public int add(int num1, int num2, int num3) {
                    return num1 + num2 + num3;
                }
                
                // Method to add two doubles
                public double add(double num1, double num2) {
                    return num1 + num2;
                }
            }
            

Example 2: Method Overloading

In this example, we have a class Printer with multiple print methods:


            public class Printer {
                // Method to print an integer
                public void print(int num) {
                    System.out.println("Printing integer: " + num);
                }
                
                // Method to print a double
                public void print(double num) {
                    System.out.println("Printing double: " + num);
                }
                
                // Method to print a string
                public void print(String text) {
                    System.out.println("Printing string: " + text);
                }
            }
            

Interview Questions on Method Overloading

  1. What is method overloading?
  2. Why is method overloading useful?
  3. Can method overloading change the return type of a method?
  4. What is the difference between method overloading and method overriding?
  5. How does Java determine which overloaded method to invoke?

Multiple Choice Questions (MCQs) - Method Overloading

  1. Which of the following statements about method overloading is true?
    1. Method overloading allows methods to have the same name and return type.
    2. Method overloading allows methods to have the same name but different return types.
    3. Method overloading allows methods to have different names but the same parameter types.
    4. Method overloading allows methods to have different names and different parameter types.
  2. What is the primary purpose of method overloading?
    1. To override superclass methods.
    2. To extend the functionality of a class.
    3. To provide multiple entry points to a method.
    4. To improve code readability.
  3. Can method overloading be applied to static methods?
    1. Yes
    2. No
    3. It depends on the access modifier of the method.
    4. Only in interfaces
  4. How does Java determine which overloaded method to invoke?
    1. By checking the method signature.
    2. By checking the return type of the method.
    3. By checking the method body.
    4. By checking the method's access modifier.
  5. What happens if two overloaded methods have the same name and parameter types?
    1. It results in a compilation error.
    2. Java selects the method with the lowest accessibility.
    3. Java selects the method with the highest accessibility.
    4. Java selects the method declared first in the class.