Top 10 Interview Questions in Overloading – Compile time Polymorphism
In this blog lets discuss about the interview questions relevant to overloading
- What is Overloading?
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
- Why Overloading is called compile time polymorphism?
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.
- What is Method Overloading?
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
- What will happen if the method with matching parameter is not found?
In the calling part, it will try to accomodate to another method by upcasting the value given, If still not possible, then it will give compiler error
- What is Constructor Overloading?
- Why do we need overloading?
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.
- How to call between the constructors in the same class?
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
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
- What is Varargs?
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
- How will you choose between Varargs and Overloading?
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
- Name few overloaded methods from the Java API?
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.