Method Overloading Interview Questions

Static Polymorphism Or Method Overloading Interview Questions: Static Polymorphism, or Method Overloading, is a fundamental concept in object-oriented programming in Java. It involves using methods with the same name but different class parameters. Static Polymorphism or Method Overloading Interview Questions are a critical part of the Java developer interview process, aimed at assessing a candidate’s knowledge and proficiency in using Method Overloading in Java, including its syntax, advantages, and best practices.

These interview questions require candidates to demonstrate their expertise in creating and implementing overloaded methods to handle different data types and scenarios. In this context, this article provides some commonly asked Static Polymorphism or Method Overloading Interview Questions in Java programming to help Java developers prepare for interviews and better understand Static Polymorphism or Method Overloading in Java.

Method Overloading Interview Questions

  • What is the other name of Method Overloading?
  • How will you implement method overloading in Java?
  • What kinds of argument variations are allowed in Method Overloading?
  • Why it is not possible to do method overloading by changing return type of method in java?
  • Is it allowed to overload main() method in Java?
  • What is Runtime Polymorphism?
  • Is it possible to achieve Runtime Polymorphism by data members in Java?
  • Explain the difference between static and dynamic binding?

Method Overloading Interview Questions And Answers

What is method overloading in Java?
Answer:

  • If any class in Java contains multiple methods with the exact 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 the same name with different input parameters then it is called as method overloading in Java

What all comprises the method signature in Java for method overloading?
Answer:
Method signature consists of below things (for overloading)

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

What are the things to consider while overloading methods in Java?
Answer:
When we overload methods in Java, the 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 the same and the combination of the number of input parameters & their data-type has to be different for successful compilation

Whether overloading is a run-time or compile-time polymorphism?
Answer:
Compile-time polymorphism, as it is resolved at compile-time

Whether method overloading is a static binding or dynamic binding?
Answer:
Static binding, as it is resolved during compile-time

What are the other names used to refer to method overloading?
Answer:
Compile-time polymorphism or Static binding

What are the ways to overload methods in Java?
Answer:
Below are the ways to overload methods in Java, by changing

  • Number of input parameters
  • Data-type of input parameters
  • Order of input parameters, if they are of different data-types

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

What are the restrictions on access modifiers in method signature while overloading in Java?
Answer:
Access modifiers doesn’t affect method overloading, so overloaded methods can have the same or different access levels

Does access modifier affect method overloading in Java?
Answer:
Access modifiers doesn’t affect method overloading, so overloaded methods can have the same or different access levels

Whether it is possible to overload methods in Java, just by changing return-type?
Answer:
No, it is not valid to consider return type for method overloading

Whether class compiles successfully if we have two methods with the same name but different return-type?
Answer:
Compilation fails with below error

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

Can a method be overloaded based on different return types but the same argument type?
Answer:
No, compilation fails

Why it is not possible to overload methods based on the return type?

  • The reason is type ambiguity
  • First of all, we cannot have two same methods with exactly the same input parameters. In this case, the compiler throws an 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

Whether it is possible to overload methods in Java, just by changing different exceptions in throws clause?

  • No, an exception doesn’t affect method overloading, so overloaded methods can have any exception or not at all
  • An exception doesn’t account for method overloading

Whether class compiles successfully if we have two methods with the same name and also same/different return-type but different throws exception?
Answer:
It depends on the number and data-type of input parameters

Can we overload static methods in Java?
Answer:
Yes, we can overload static method

Can we overload main() methods in Java?

  • Yes, we can overload the main() method which is static
  • But entry point to JVM will remain the same main() method with below signature
  • Public static void main(String args[])

Can we overload constructor similar to method overloading?
Answer:
Yes, constructor overloading is possible but that is different to method overloading

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 don’t account into method overloading

Can we overload methods that differ only by static keyword?
Answer:
No, compilation fails as non-access modifiers like static is not considered for method overloading

Whether class compiles if we have two methods with exactly the 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 an error for the Duplicate method as shown in the below screen capture

Why method overloading required in Java

  • Suppose, if we want to perform similar kind of tasks and their operation differ only by a number of parameters or their data-types or both then method overloading is the best concept to apply
  • Maintains consistency with method naming for a similar type of tasks
  • Increases the readability of the program
  • This helps the developer to invoke the method with the same name but changing required arguments in the required order with their corresponding data-types
  • Example: java.lang.String class from java.lang package contains 9 overloaded ‘valueOf()’ method with a different number of input parameters or their data-types

Can an overloaded method override?
Answer:
Yes, as far as it abides/comply with overriding rules (See overriding rules here)

What is the difference between method overloading v/s method overriding in Java?
Answer:
Refer Method overriding v/s method overriding in Java

References: Article

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment