When class has multiple methods with same name but different parameters, it is known as method overloading.
Here different parameters means
1 Either number of parameters can be different
2 Or type of parameters can be different
For Example,
In below program we have three overloading methods named as testMethod()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
publicclassPumpkinDemo {
publicstaticvoidmain(String[] args) {
//it will call first method as we have passed int parameter
testMethod(1);
// it will call second method as we have passed String parameter
testMethod("pumpkin");
// it will call third method
// as we have passed two parameters and
// there are no other methods with two parameters
testMethod(1,"pumpkin");
}
//first method
publicstaticvoidtestMethod(intnumber)
{
System.out.println("number");
}
//second method
publicstaticvoidtestMethod(String str)
{
System.out.println("String");
}
//third method
publicstaticvoidtestMethod(intnumber, String str)
{
System.out.println("number, String");
}
}
|
Output
1
2
3
|
number
String
number, String
|
Why It Is Known As Static Binding?
In case of method overloading, JVM can decide which method to be called at compile time based on the number and type of parameters, right?
In other words, method calls are resolved at compile time in case of method overloading. That is why it is also known asStatic Binding or Static Polymorphism or Compile time Polymorphism.
Tricky Method Overloading Programming Questions
Let's move further with tricky programming questions. We have listed down all such questions and their answers with explanation. But we suggest you guys to first try solving and guessing the answer of each question and then read its answer.
Question 1 What would be the output of below code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
publicclassPumpkinDemo {
publicstaticvoidmain(String[] args) {
testMethod(1);
}
publicstaticvoidtestMethod(longnumber)
{
System.out.println("long");
}
publicstaticvoidtestMethod(intnumber)
{
System.out.println("int");
}
publicstaticvoidtestMethod(Integer number)
{
System.out.println("Integer");
}
publicstaticvoidtestMethod(doublenumber)
{
System.out.println("double");
}
publicstaticvoidtestMethod(Number number)
{
System.out.println("Number");
}
}
|
Answer
It will print output as 'int'. Here is the preference hierarchy
In other words,
3 if we will remove method with int parameter, then output will be long,
4 if we will remove methods with int and long parameter, then output will be double, and so on.
Question 2) In above program, if I want to pass value as 1, but I want to call each method one by one. Write main() method code to get below output:
1
2
3
4
|
int
long
double
Integer
|
Answer
1
2
3
4
5
6
|
publicstaticvoidmain(String[] args) {
testMethod(1);
testMethod(1L);
testMethod(newInteger(1));
testMethod(1.0);
}
|
Question 3) What would be the output of below code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
publicclassPumpkinDemo {
publicstaticvoidmain(String[] args) {
testMethod(1);
}
publicstaticvoidtestMethod(bytenumber)
{
System.out.println("byte");
}
publicstaticvoidtestMethod(shortnumber)
{
System.out.println("short");
}
}
|
Answer
It will give compile time error as jvm can't downcast from int to byte by itself. If you will manually downcast while calling the method like testMethod((byte)1), then only it will get compiled and will give you output as byte.
Question 4) What would be the output of below code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
publicclassPumpkinDemo {
publicstaticvoidmain(String[] args) {
testMethod(null);
}
publicstaticvoidtestMethod(Long number)
{
System.out.println("Long");
}
publicstaticvoidtestMethod(Integer number)
{
System.out.println("Integer");
}
}
|
Answer
It will give compile time error. null creates ambiguity here as Long and Integer both can be null and JVM can not decide the method to be called.
Question 5) In above example, I want to pass null as a argument, but still i want method with String type to be called i.e. output should be String. What changes do I need to make in my main method?
Answer
1
2
3
4
|
publicstaticvoidmain(String[] args) {
String s =null;
testMethod(s);
}
|
Question 6) Output of below code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
publicstaticvoidmain(String[] args) {
testMethod(null);
}
publicstaticvoidtestMethod(Number number)
{
System.out.println("Number");
}
publicstaticvoidtestMethod(Integer number)
{
System.out.println("Integer");
}
}
|
Answer
Number is a parent class and Integer is its child class, so it will not create ambiguity. Priority is given to the the lowest child class i.e. Integer in our case. So answer is 'Integer'.
Comments
Post a Comment