Overloading in java

In Interview, you can expect a bunch of questions over overloading and believe me those are very tricky, If you are not understand overloading then it will be very tough to crack interview as this is the fundamentals of Java.

NB: Many people think overloading is very easy but go through the article you will find it is not so easy when overloading comes with boxing,varags, and Widening. 


In this article, we are going to discuss Overloading and it's complexity.

By definition, if two or more methods in a class or in a parent class and child class have the same name but different data signature irrespective of return type called Overloading.


so following example is a  valid overloading is

public class OverLoadtest {


    public void test(int i)
    {
        System.out.println("overloaded 1");
    }

    public void test(long j)
    {
        System.out.println("overloaded 2");
    }

    public static void main(String[] args) {
   
        OverLoadtest test = new OverLoadtest();
   
    }

}

Overloading has some contracts when you are doing a over loading please remember this contracts.

a.  Overloaded methods MUST change the argument list.
b.  Overloaded methods CAN change the return type.
c. Overloaded methods CAN change the access modifier.
d  Overloaded methods CAN declare new or broader checked exceptions.

so

public void do(int i) throws AirithmeticException;
public void do(long j)
public int do(double j)
private int do(String j)
private void do()
throws Exception;

all are valid overloading

overloading in java
Add caption




Now we are talking about some puzzle over overloading

a. doX(int i,long j)
   doX(long j,int i,)
is this  a valid overloading???

The answer is Yes Data signature is different although method meaning is same as only arguments are swap their space.

b. doX(int i)
doX(Integer i) and we call by doX(2);

can you guess which method should be called?

Think......

It will call do(int i)
 as in overloading precedence always over in primitive type;

c. doX(Integer i);
doX(int ... i); now calling doX(2);

 can you tell which version will be called?

It calls do(Integer i); as in overloading, Boxing has precedence over varargs.

d. doX(Integer i) and doX(long i) calling with doX(2)

now here it will call do(long i) as in overloading primitive widening has precedence over boxing .

e. do(Object i)
do(Long i)
 call with do(2) can you tell which version will be called

it calls do(Object i)  version
In Overloading Boxing then widening is permissible, mean here conversion is
int->Integer->Object (boxing then Widening)
 but do(Long i) int->long->Long widening then boxing will not allow.

so remember the following rules to determine which version will be called.

1. Primitive widening uses the "smallest" method argument possible.
2. Used individually, boxing and var-args are compatible with overloading.
3. You can't widen from one wrapper type to another. (IS-A fails.)
4. You can't widen and then box. (An int can't become a Long.)
5. You can box and then widen. (An int can become an Object, via Integer.)
6. You can combine var-args with either widening or boxing.

see Overriding related interview questions also.





Post a Comment