Java find output programs (Overloading) | set 2

Find the output of Java programs | Overloading | Set 2: Enhance the knowledge of Java Overloading concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 03, 2021

Question 1:

public class ClsOver {
  void addValues(int val1, double val2) {
    double res = 0;

    res = val1 + val2;

    System.out.println("###Sum: " + res);
  }

  void addValues(double val1, int val2) {
    double res = 0;

    res = val1 + val2;

    System.out.println("@@@Sum: " + res);
  }

  public static void main(String[] args) {
    ClsOver Ob = new ClsOver();

    Ob.addValues(20, 30.6);
    Ob.addValues(20.6, 30);

  }
}

Output:

###Sum: 50.6
@@@Sum: 50.6

Explanation:

In the above program, we overloaded the addValues() method based on a different order of arguments. In the Main() method we created the object of ClsOver class and call both overloaded methods that will print the additions of given arguments on the console screen.

Question 2:

public class ClsOver {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val2;

    System.out.println("###Sum: " + res);
  }

  void addValues(long val1, long val2) {
    int res = 0;

    res = val1 + val2;

    System.out.println("@@@Sum: " + res);
  }

  public static void main(String[] args) {
    ClsOver Ob = new ClsOver();

    Ob.addValues(20, 30);
    Ob.addValues(40L, 50L);

  }
}

Output:

/ClsOver.java:13: error: incompatible types: possible lossy 
conversion from long to int
    res = val1 + val2;
               ^
1 error

Explanation:

The above program will generate a syntax error because of the below method,

void addValues(long val1, long val2)
{
    int res = 0;

    res = val1 + val2;

    System.out.println("@@@Sum: " + res);
}

In the above method variable res is int type, but val1 and val2 are long types and we know that we cannot store the sum of two long values into integer variable res that's why error gets generated by the above program.

Question 3:

public class ClsOver {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val2;

    System.out.println("###Sum: " + res);
  }

  void addValues(short val1, short val2) {
    short res = 0;

    res = val1 + val2;

    System.out.println("@@@Sum: " + res);
  }

  public static void main(String[] args) {
    ClsOver Ob = new ClsOver();

    Ob.addValues(20, 30);
    Ob.addValues(40H, 50H);

  }
}

Output:

/ClsOver.java:22: error: ')' expected
    Ob.addValues(40H, 50H);
                   ^
/ClsOver.java:22: error: ';' expected
    Ob.addValues(40H, 50H);
                    ^
/ClsOver.java:22: error: not a statement
    Ob.addValues(40H, 50H);
                        ^
/ClsOver.java:22: error: ';' expected
    Ob.addValues(40H, 50H);
                         ^
4 errors

Explanation:

The above program will generate compile-time errors. In the above program, we overloaded the addValues() method based on different types of arguments. Here, the first method is used to integer arguments and the second method used to add arguments of short type.

Ob.addValues(40H, 50H);

The above method call used 'H' as a suffix but it is not valid for short type variables that's why compile-time errors get generated.

Question 4:

class ABC {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val2;

    System.out.println("Sum: " + res);
  }
}

class XYZ extends ABC {
  void addValues(float val1, float val2) {
    float res = 0;

    res = val1 + val2;

    System.out.println("Sum: " + res);
  }
}

public class ClsOver {
  public static void main(String[] args) {
    XYZ ob = new XYZ();

    ob.addValues(40, 60);
    ob.addValues(40.3F, 60.7F);
  }
}

Output:

Sum: 100
Sum: 101.0

Explanation:

In the above program, we created three classes ABC, XYZ, and ClsOver. The ABC class contains addValues() method to add integer values and then we inherited the ABC class into XYZ class. The XYZ also contains the addValues() method to add float value.

Now look to the ClsOver class - the ClsOver class contains a main() method which is the entry point of the program. Here, we created the object ob of XYZ class. Then we called both addValues() method that will print the sum of integer and sum of float values respectively.

Question 5:

class ABC {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val2;

    System.out.println("Sum: " + res);
  }
}

class XYZ extends ABC {
  void addValues(float val1, float val2) {
    float res = 0;

    res = val1 + val2;

    System.out.println("Sum: " + res);
  }
}

public class ClsOver {
  public static void main(String[] args) {
    ABC ob = new ABC();

    ob.addValues(40, 60);
    ob.addValues(40.3F, 60.7F);
  }
}

Output:

/ClsOver.java:26: error: incompatible types: possible lossy 
conversion from float to int
    ob.addValues(40.3F, 60.7F);
                 ^
Note: Some messages have been simplified; recompile with 
-Xdiags:verbose to get full output
1 error

Explanation:

In the above program, we created three classes ABC, XYZ, and ClsOver. The ABC class contains addValues() method to add integer values and then we inherited the ABC class into XYZ class. The XYZ also contains the addValues() method to add float value.

Now look to the ClsOver class - the ClsOver class contains a main() method which is the entry point of the program. Here, we created the object ob of ABC class. But the addValues() method of the class 'ABC' can accept integer arguments only. But here we called a method with float arguments that's why compile-time error gets generated by the above program.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.