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.
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Answer Question 1:
Output:
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.
Answer Question 2:
Output:
Explanation:
The above program will generate a syntax error because of the below method
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.
Answer Question 3:
Output:
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.
Answer Question 4:
Output:
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.
Answer Question 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer