Method Overloading Part 2 Tutorial

In this tutorial I will be discussing how primitive type promotion affects which overloaded method is called based on the data type of the arguments. If you are unclear on the concept of type promotion, please review my Primitive Numeric Type Promotion Tutorial before continuing. If an overloaded method exists with the exact data type parameters as the data type of the arguments, then that overloaded method will be called. However, if no overloaded methods exist for the data type of the arguments and the arguments are byte, short, or char, then they are promoted to an int data type and the overloaded constructor with int parameters will be used. Float will promote to double and int will promote to long if no matching signatures exist.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md MethodOverloadingTwo
C:\Java>cd MethodOverloadingTwo
C:\Java\MethodOverloadingTwo>Notepad MethodOverloadingTwo.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class MethodOverloadingTwo {
    public static void main(String args[]) {
        byte bL = 5, bH = 5, bW = 5;
        System.out.println("Box with byte arguments, volume = " + new Box().calculateVolume(bL, bH, bW));
        
        short sL = 6, sH = 6, sW = 6;
        System.out.println("Box with short arguments, volume = " + new Box().calculateVolume(sL, sH, sW));

        char cL = 7, cH = 7, cW = 7;
        System.out.println("Box with char arguments, volume = " + new Box().calculateVolume(cL, cH, cW));

        int iL = 8, iH = 8, iW = 8;
        System.out.println("Box with int arguments, volume = " + new Box().calculateVolume(iL, iH, iW));

        long lL = 9L, lH = 9L, lW = 9L;
        System.out.println("Box with long arguments, volume = " + new Box().calculateVolume(lL, lH, lW));

        float fL = 5.3f, fH = 5.4f, fW = 5.5f;
        System.out.println("Box with float arguments, volume = " + new Box().calculateVolume(fL, fH, fW));

        double dL = 6.3d, dH = 6.4d, dW = 6.5d;
        System.out.println("Box with double arguments, volume = " + new Box().calculateVolume(dL, dH, dW));
    }
}

class Box {
    Box() {
        super();
        System.out.println("----------");
    }

    //after running once, comment this method out and run again
    int calculateVolume(byte length, byte height, byte width) {
        System.out.println("In byte parameter overloaded method");
        return (length * height * width);
    }  
    //after running once, comment this method out and run again
    int calculateVolume(short length, short height, short width) {
        System.out.println("In short parameter overloaded method");
        return (length * height * width);
    }  
    //after running once, comment this method out and run again
    int calculateVolume(char length, char height, char width) {
        System.out.println("In char parameter overloaded method");
        return (length * height * width);
    }  

    int calculateVolume(int length, int height, int width) {
        System.out.println("In int parameter overloaded method");
        return (length * height * width);
    }
    
    long calculateVolume(long length, long height, long width) {
        System.out.println("In long parameter overloaded method");
        return (length * height * width);
    }  
    //after running once, comment this method out and run again
    float calculateVolume(float length, float height, float width) {
        System.out.println("In float parameter overloaded method");
        return (length * height * width);
    } 

    double calculateVolume(double length, double height, double width) {
        System.out.println("In double parameter overloaded method");
        return (length * height * width);
    } 
}

Now switch back to the command prompt (CMD) and type in javac MethodOverloadingTwo.java and press Enter.
Now type in java MethodOverloadingTwo and press Enter.


C:\Java\MethodOverloadingTwo>javac MethodOverloadingTwo.java
C:\Java\MethodOverloadingTwo>java MethodOverloadingTwo
----------
In byte parameter overloaded method
Box with byte arguments, volume = 125
----------
In short parameter overloaded method
Box with short arguments, volume = 216
----------
In char parameter overloaded method
Box with char arguments, volume = 343
----------
In int parameter overloaded method
Box with int arguments, volume = 512
----------
In long parameter overloaded method
Box with long arguments, volume = 729
----------
In float parameter overloaded method
Box with float arguments, volume = 157.41
----------
In double parameter overloaded method
Box with double arguments, volume = 262.08


Final thoughts

As you can see, overloaded methods are super easy to implement and the java compiler takes care of selecting the proper method to invoke. Type promotion applies not just to the arithmetic operations, but to method overloading as well.


Tutorials