Vijay Kannan Vijay Kannan - 3 months ago 8
Java Question

Overloaded method resolution at runtime and how type promotion works?

package arunjava;

public class sample3 {

public static void main(String[] args) {
Box25 b1 = new Box25();
Box25 b2 = new Box25();


b1.Dimension(25, 32, 65);
b2.Dimension(25, 45, 62);

System.out.println("volume is" + b1.volume());
System.out.println("volume is" + b2.volume());

b1.Dimension(4, 6, 8);
b2.Dimension(6, 8, 4);

System.out.println("volume is" + b1.vol());
System.out.println("volume is" + b2.vol());
}
}

class Box25 {
double height, width, depth;

int height1, width1, depth1;

public void Dimension(double height, double width, double depth) {
this.height = height;
this.width = width;
this.depth = depth;
}

public void Dimension(int height1, int width1, int depth1) {
this.height1 = height1;
this.width1 = width1;
this.depth1 = depth1;
}

double volume() {
return height * depth * width;
}

int vol() {
return height1 * depth1 * width1;
}
}


Hey guys I had created the following programme in my java tutorial. The problem is that I am not able to compute the volume of the box which has the double datatype.

The programme result shows the following:

volume is0.0
volume is0.0
volume is192
volume is192


Secondly, I have a doubt in the java concept Method overloading, as I know that in method overloading we can use the same method name with different parameters but as I created the volume method, I had to modify the name of the methods(volume,vol), so as to overload the volume method and get the answer. Why is it like that.

Eagerly waiting for your reply guys as I am just new to the java programming language. I would a;so request you to paste the corrected code while answering.

Answer

Following lines invokes

b1.Dimension(25, 32, 65);
b1.Dimension(4, 6, 8);

invokes method public void Dimension(int height1, int width1, int depth1) because the literal's 25, 32 and 65 are treated of type int

So, the fields height, width, depth of class Box25 have default values 0.0 hence the output you get is 0.

There are various ways you can fix the problem, by invoking appropriate method

Approach 1:

b1.Dimension((double)25, (double)32, (double)65);
b1.Dimension((double)4, (double)6, (double)8);

Approach 2:

double h = 25, w = 32, d = 65;
b1.Dimension(25, 32, 65);

Approach 3:

b1.Dimension(25.0, 32.0, 65.0);
b1.Dimension(4.0, 6.0, 8.0);

Why are the int arguments passed, not promoted to double?

  • At runtime, the resolution first happens by exact match of argument types
  • If the matching types are found, then the method with exact matched arguments is invoked
  • If there no exact match of arguments, then the types are promoted to next higher type and if there is a match, then that method is invoked.

Example 1:

public class OverloadingDemo {

    public static void main(String args[]) {
        OverloadingDemo obj = new OverloadingDemo();
        obj.sayHello(10);
    }

    public void sayHello(double x) {
        System.out.println("Hello double  " + x);
    }

}

Output: Hello double 10.0

Reason: Since there is no matching method for value 10 (which is of type int), it is promoted to long. As there is no matching method which accepts long argument, it is promoted to double. Now that there is a method with accepts double argument, it is invoked.

Example 2:

public class OverloadingDemo {

    public static void main(String args[]) {
        OverloadingDemo obj = new OverloadingDemo();
        obj.sayHello(10);
    }

    public void sayHello(int x) {
        System.out.println("Hello int  " + x);
    }

    public void sayHello(double x) {
        System.out.println("Hello double  " + x);
    }

}

Output: Hello int 10

Reason: Since there is a matching method for value 10 (which is of type int), it is invoked.

Example 3:

public class OverloadingDemo {

    public static void main(String args[]) {
        OverloadingDemo obj = new OverloadingDemo();
        obj.sayHello(10);
    }

    public void sayHello(long x) {
        System.out.println("Hello long  " + x);
    }

    public void sayHello(double x) {
        System.out.println("Hello double  " + x);
    }

}

Output: Hello long 10

Reason: Since there is no matching method for value 10 (which is of type int), it is promoted to long. Now that there is a method with accepts long argument, it is invoked.

Comments