No comments yet

Top 10 Interview Questions in Overloading

[Social9_Share type="horizontal"]

Top 10 Interview Questions in Overloading – Compile time Polymorphism

In this blog lets discuss about the interview questions relevant to overloading

  1. What is Overloading?
  2. Overloading comes under polymorphism. Polymorphism is one of the Object Oriented Programming concept.
    In overloading, the method or the constructor can be overloaded with 0 to any number of arguments. In case of method it is method overloading and in case of constructor it is called constructor overloading. Java supports both constructor and method overloading. Overloading is also called as compile time polymorphism

  3. Why Overloading is called compile time polymorphism?
  4. When the method gets called, it tries to pair with the corresponding method definition during the compile time after checking for the parameter. If the method definition is not available, it gives compiler error. So it is called as compile time polymorphism.

  5. What is Method Overloading?
  6. In method overloading the method is overloaded with zero to any number of arguments.

    Rules for method overloading

    • Overloading happens in the same class.
    • The method name is same.
    • Either the number of parameters or the datatype of the parameter should be different
    • The return type and the access specifier of the method can be any


  7. What will happen if the method with matching parameter is not found?
  8. In the calling part, it will try to accommodate to another method by upcasting the value given, If still not possible, then it will give compiler error

  9. What is Constructor Overloading?
  10. In constructor overloading, the class can have zero to many constructors, overloaded with different parameters.Say for example, if I have an Employee class with two instance variables as String name and double salary, then, I can have a constructor with no parameters, with one or two parameters

  11. Why do we need overloading?
  12. It is very difficult to remember multiple names that perform relatively similar tasks. Say for example if i want to calculate the area of different shapes – if there is no overloading concept, then i should have different method names as areaTriangle(),areaRect(), areaSquare(), areaCircle() and so on. But with overloading i can have one method name as area() – overload it with different parameters and calculate the area of different shapes based on the number or the datatype of the parameter(eg. area(double) for circle, area(int) for square and so on).
    So, overloading helps us in not creating multiple method names for similar tasks.

    Become an expert Java Developer

  13. How to call between the constructors in the same class?
  14. Use this() with matching parameters to call the appropriate constructors in the same class.
    The purpose of this() is avoid rewriting the code again(while initializing the instance variables). Let me show an example without and with this() keyword

    Without this()

    Using this()

    Remember either this() or super() can be in the first line of the constructor. In this case the super class constructor gets called from one parameter constructor

  15. What is Varargs?
  16. Varargs is variable-argument list. This argument is a dynamic array to which you can pass zero to many values while calling the method having this parameter. This varargs though has some rules.

    • You can have only one var-args in an array
    • The var-args parameter has to be the last parameter
    • The datatype given for the parameter should be followed by three dots


  17. How will you choose between Varargs and Overloading?
  18. Overloading is used when the method name is same, but the method parameters and functionality are different.
    eg. calculating area for different shapes

    Var-args list is used when the functionality is same but the method values are different for different method call.
    eg. calculating sum of marks which may vary from one subject to many subjects for students

  19. Name few overloaded methods from the Java API?
  20. There are many methods available in Java APIs. For Example the toString() and valueOf() method from java.lang.Integer class is overloaded as shown below.

    java overloading

    If you have some more questions relevant to overloading, then do share with us.To learn about the top questions and answers in inheritance click here.

    Happy Learning

Post a comment

{"wp_error":"cURL error 28: Resolving timed out after 5513 milliseconds"}