Thursday, January 30, 2014

Something about Overloading...

Overloading allows to use same name of method for multiple methods provided signatures are different. Signatures can be different in number, in type or in order. Example below shows.

public class OverloadDemo {
    public int add(int x, int y){
        return x+y;
    }
    public int add(int x, int y, int z){
        return x+y+z;
    }

    public double add(int x, double y){
        return x+y;
    }

    public double add(double x, int y){
        return x+y;
    }

    public static void main(String[] args) {
        OverloadDemo obj = new OverloadDemo();

        System.out.println(obj.add(10,20));
        System.out.println(obj.add(10,20,56));
        System.out.println(obj.add(10,20.9));
        System.out.println(obj.add(10.7,20));
    }

}

Output :

run:
30
86
30.9
30.7

It is simple. It is a user-friendly feature of Java and all Object-Oriented Programming languages.

Here is a small twist. See the code ...

public class AnotherOverload {
 
    int add (int x, int y){
        System.out.println("Int Overload");
        return x+y;
    }
    int add (short x, short y){
        System.out.println("Short Overload");
        return x+y;
    }
    long add(long x, long y){
        System.out.println("Long overload");
        return x+y;
    }
    double add(double x, double y){
        System.out.println("double overload");
        return x+y;
    }
    float add(float x, float y){
        System.out.println("double overload");
        return x+y;
    }
    public static void main(String[] args) {
        AnotherOverload ob = new AnotherOverload();
        System.out.println("sum of 10 + 20 is "+ob.add(10,20));
        System.out.println("sum of 10.1 + 20.1 is "+ob.add(10.1,20.2));
    }
}
Output:

Int Overload
sum of 10 + 20 is 30
double overload
sum of 10.1 + 20.1 is 30.299999999999997

There are two Questions over here :

10 and 20 are short type values according to the range, rather I can say it falls in the range of byte. But the method is called with an int parameters. Likewise 10.1 and 20.1 are float values but the call is for double. All non-real numeric literals by default are int and all real literals are double.

Here is another interesting scenario...

class IntOverload {
public static void main(String args[]) {
int x=10,y=20;
                short a=5, b=7;

IntOverload ob = new IntOverload();
System.out.println(ob.add(x,y));
        System.out.println(ob.add(a,b));
        
}
        
        short add(short a, short b){
            System.out.println("Short");
            return a+b;
        }

long add(long a, long b){
System.out.println("long");
return a+b;
}

float add(float a, float b){
System.out.println("float");
return a+b;
}

double add(double a, double b){
System.out.println("double");
return a+b;
}
}

This code does not compile and give an error :

When add method is called with explicit short type, it gives compile time error "Required :short, found:int".

All integer type numerics are promoted to int type at the time of computation. So the solution can be achieved by changing the method code. We use explicit cast... just a word of caution when overloading...

 short add(short a, short b){
            System.out.println("Short");
            return (short)(a+b);
        }

Enjoy coding... :)

1 comment: