Interview Question and Answers on Java Method Overloading

In this article, we will cover some of the interview questions with their justification on method overloading

These are most frequently asked interview question from OOPS concepts

Before reading interview question & answers, go through Java Method Overloading concepts

 

Q) What is method overloading in Java?

–> If any class in Java contains multiple methods with exactly same name but with different input parameter lists then it is known as method overloading in Java

–> In other words, if 2 or more methods in Java class have same name with different input parameters then it is called as method overloading in Java

Q) What all comprises method signature in Java for method overloading?

–> Method signature consists of below things (for overloading)

  • method name (should be same)
  • number of input parameters
  • data-type of input parameters

Q) What are the things to consider while overloading methods in Java?

–> When we overload methods in Java, compiler checks 3 things

  1. method name (should be same)
  2. number of input parameters
  3. data-type of input parameters

–> Method name has to be same and combination of number of input parameters & their data-type has to be different for successful compilation

Q) Whether overloading is a run-time or compile-time polymorphism? (1)

–> Compile-time polymorphism, as it is resolved at compile-time

Q) Whether method overloading is a static binding or dynamic binding? (2)

–> Static binding, as it is resolved during compile-time

Q) What are the other names used to refer method overloading? (3)

–> Compile-time polymorphism or Static binding

Q) What are the ways to overload methods in Java?

–> Below are the ways to overload methods in Java, by changing

  1. Number of input parameters
  2. Data-type of input parameters
  3. Order of input parameters, if they are of different data-types

Note: return type is not valid to consider for overloading concepts

Q) What are the restrictions on access modifier in method signature while overloading in Java? (1)

–> Access modifiers doesn’t affect method overloading, so overloaded methods can have same or different access levels

Q) Does access modifier affects method overloading in Java? (2)

–> Access modifiers doesn’t affect method overloading, so overloaded methods can have same or different access levels

Q) Whether it is possible to overload methods in Java, just by changing return-type? (1)

–> No, it is not valid to consider return type for method overloading

Q) Whether class compiles successfully, if we have two methods with same name but different return-type? (2)

–> Compilation fails with below error

Error: Duplicate method method_Name(input parameters) in type ClassName

3_Method_Overloading_duplicate_method_error

 

Q) Can a method be overloaded based on different return type but same argument type? (3)

–> No, compilation fails

Q) Why it is not possible to overload methods based on the return type? (4)

–> Reason is type ambiguity

–> First of all, we cannot have two same methods with exactly same input parameters. In this case, compiler throws error

–> It is a compile-time error, as it is resolved during compile-time

–> Also, it is very difficult for JVM to understand as to which version of overloaded methods to call

Q) Whether it is possible to overload methods in Java, just by changing different exception in throws clause? (1)

–> No, exception doesn’t affect method overloading, so overloaded methods can have any exception or not at all

–> Exception doesn’t accounted for method overloading

Q) Whether class compiles successfully, if we have two methods with same name and also same/different return-type but different throws exception? (2)

–> It depends on the number and data-type of input parameters

Q) Can we overload static methods in Java?

–> Yes, we can overload static method

Q) Can we overload main() methods in Java?

–> Yes, we can overload main() method which is static

–> But entry point to JVM will remain same main() method with below signature

–>public static void main(String args[])

Q) Can we overload constructor similar to method overloading?

–> Yes, constructor overloading is possible but that is different to method overloading

Q) Can we declare overloaded methods as final?

–> Yes, access-modifier (private, default, protected, public) doesn’t affect method overloading

–> Similarly, non-access modifiers like final, static, transient, synchronized, volatile, abstract, strictfp doesn’t accounted into method overloading

Q) Can we overload methods that differ only by static keyword?

–> No, compilation fails as non-access modifiers like static is not considered for method overloading

Q) Whether class compiles, if we have two methods with exactly same method signature (method name, same input parameter and their data-types) but one with static keyword and other non-static?

–> No, compilation fails as non-access modifiers like static is not considered for method overloading

–> Compiler throws error for Duplicate method as shown in the below screen capture

10_Method_Overloading_Interview_question_static_keyword

Q) Why method overloading required in Java

–> Suppose, if we want perform similar kind of tasks and their operation differ only by number of parameters or their data-types or both then method overloading is the best concept to apply

–> Maintains consistency with method naming for similar type of tasks

–> Increases the readability of the program

–> This helps the developer to invoke method with same name but changing required arguments in required order with their corresponding data-types

–> Example: java.lang.String class from java.lang package contains 9 overloaded ‘valueOf()’ method with different number of input parameters or their data-types

Q) Can overloaded method be override?

–> Yes, as far as it abide/comply with overriding rules (See overriding rules here)

Q) What is the difference between method overloading v/s method overriding in Java?

–> Refer Method overriding v/s method overriding in Java

 

Read also:

 

References:

https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Happy Coding !!
Happy Learning !!