0

The following code is correct with respect to method overloading.

  public class class7A {
  public static void main(String[] args) {
    testing obj_1 = new testing(); 
    int a=12,b=14,c=20;
    obj_1.func1(a,b,c); //invokes the 3rd method in the testing class
                                         }
                       }

class testing{
void func1(int a,int b){
    System.out.println("The values of length and breadth entered for the box is  "+a+" "+b);
                       }
void func1(int a){
    System.out.println("We can only talk about length here folks which is "+a);
                }
void func1(double a,double b,double c){  //This method is invoked
    System.out.println("The value of length ,breadth and height is "+a+","+b+","+c+" respectively");
                                      }
             }

Now the explanation given for the fact that the 3rd method is invoked even when the parameters defined for the 3rd method are "double" is that java automatically converts double into int here.I also know java does any operation on the primitive type by first converting the types into int at the back end which holds true for bytes as well. However when i change the parameters of the 3rd method to be of byte type instead of double,the code gives an error .Foe example the code below gives an error :

Why it is happening so ?

  public class class7A {
  public static void main(String[] args) {
    testing obj_1 = new testing(); 
    int a=12,b=14,c=20;
    obj_1.func1(a,b,c); 
                                         }
                       }

class testing{
void func1(int a,int b){
    System.out.println("The values of length and breadth entered for the box is  "+a+" "+b);
                       }
void func1(int a){
    System.out.println("We can only talk about length here folks which is "+a);
                }
void func1(byte a,byte b,byte c){ //This gives error
    System.out.println("The value of length ,breadth and height is "+a+","+b+","+c+" respectively");
warrior_monk
  • 383
  • 2
  • 14
  • *"java automatically converts double into int here"* No, it doesn't – Tom Apr 21 '17 at 03:48
  • @Tom ,if it does not can you explain,why the int arguments are working for parameters defined to be of type "double" ? – warrior_monk Apr 23 '17 at 05:36
  • Just look at ___your own___ code. Why should it convert double to int, when int is the source type and double the target type? Makes no sense to convert backwards. – Tom Apr 23 '17 at 14:15

1 Answers1

0

You must cast the type of data int to byte when you pass as argument of the method.

example:

public class class7A {
    public static void main(String[] args) {
      testing obj_1 = new testing();
      int a = 12, b = 14, c = 20;

      obj_1.func1((byte) a, (byte) b, (byte) c);
    }
}

class testing {
    void func1(int a, int b) {
       System.out.println("The values of length and breadth entered for the box is  " + a + " " + b);
    }

    void func1(int a) {
         System.out.println("We can only talk about length here folks which is " + a);
    }

    void func1(byte a, byte b, byte c) { // This gives error
         System.out.println("The value of length ,breadth and height is " + a + "," + b + "," + c + " respectively");
    }
}

and if you want to make another type of conversion you can check this post where it is explained more detailed how to convert from int to byte

https://stackoverflow.com/a/842900/7179674

Community
  • 1
  • 1