Find the output of Java programs | Overloading | Set 1: Enhance the knowledge of Java Overloading concepts by solving and finding the output of some Java programs.
Question 1:
public class ClsOver {
static void addValues(int val1, int val2) {
int res = 0;
res = val1 + val1;
System.out.println("Sum of integers: " + res);
}
static void addValues(float val1, float val2) {
float res = 0;
res = val1 + val2;
System.out.println("Sum of floats: " + res);
}
public static void main(String[] args) {
addValues(20, 30);
addValues(20.6, 30.4);
}
}
Question 2:
public class ClsOver {
void addValues(int val1, int val2) {
int res = 0;
res = val1 + val1;
System.out.println("Sum of integers: " + res);
}
void addValues(float val1, float val2) {
float res = 0;
res = val1 + val2;
System.out.println("Sum of floats: " + res);
}
public static void main(String[] args) {
addValues(20, 30);
addValues(20.6, 30.4);
}
}
Question 3:
public class ClsOver {
void addValues(int val1, int val2) {
int res = 0;
res = val1 + val1;
System.out.println("###Sum of integers: " + res);
}
int addValues(int val1, int val2) {
int res = 0;
res = val1 + val1;
System.out.println("@@@Sum of integers: " + res);
}
public static void main(String[] args) {
ClsOver Ob = new ClsOver();
Ob.addValues(20, 30);
}
}
Question 4:
public class ClsOver {
void addValues(int val1, int val2) {
int res = 0;
res = val1 + val2;
System.out.println("###Sum of integers: " + res);
}
int addValues(int val1, int val2, int val3) {
int res = 0;
res = val1 + val2 + val3;
System.out.println("@@@Sum of integers: " + res);
}
public static void main(String[] args) {
ClsOver Ob = new ClsOver();
Ob.addValues(20, 30);
Ob.addValues(20, 30, 40);
}
}
Question 5:
public class ClsOver {
void addValues(int val1, int val2) {
int res = 0;
res = val1 + val2;
System.out.println("Sum of integers: " + res);
}
int addValues(int val1, int val2, int val3) {
int res = 0;
res = val1 + val2 + val3;
return res;
}
public static void main(String[] args) {
ClsOver Ob = new ClsOver();
int result = 0;
Ob.addValues(20, 30);
result = Ob.addValues(20, 30, 40);
System.out.println("Result : " + result);
}
}
Answer Question 1:
Output:
Explanation:
The above program will generate syntax error because of the below method call,
addValues(20.6,30.4);
In the above method call, we passed arguments of double types but we did not define any method with double arguments, we defined the method with float arguments. Here, we need to add suffix 'F' for float arguments.
The correct method call is given below:
addValues(20.6F,30.4F);
Answer Question 2:
Output:
Explanation:
The above program will generate compile-time errors because we called non-static methods addValues() without creating an object in the static method main(). We cannot call non-static methods inside the static method of the class.
Answer Question 3:
Output:
Explanation:
The above program will generate a syntax error. In the above program, we created two AddValues() methods with the same name and same list of arguments but the return type is different. We cannot overload a method only based on the return type.
Answer Question 4:
Output:
Explanation:
The above program will generate syntax error because of the below method,
The above method should return an integer value but we did not return any value from the above method that's why error gets generated by the above program.
Answer Question 5:
Output:
Explanation:
In the above program, we created a class ClsOver, here we overloaded the addValues() method. The first overloaded method took 2 arguments then add both argument values and print them on the console screen. Another overloaded method took 3 arguments and return the addition of all arguments.
Now look to the main() method, here we created object Ob of class ClsOver and the called both overloaded method and print the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer