Identify and explain which type of polymorphism technique is
used in the
following code
A-
class Calculator {
public void add(int i, int j) {
System.out.println("Integer addition");
}
public void add(int i, double j) {
System.out.println("Integer and double addition");
}
public void add(double i, double j) {
System.out.println("Double addition");
}
}
B-
public class MyClass {
void test(int a) {
System.out.println("a: " + a);
}
void test(int a, int b) {
System.out.println("a and b: " + a + "," + b);
}
double test(double a) {
System.out.println("double a: " + a);
return a * a;
}
}
Answer : Compile time Polymorphism
Explanation :
Compile time polymorphism is also known as method overloading, in method overloading, one method can repeated many times as it wants with the different number of argument, different types of the datatype, and different return types, which is said to be as the compile time polymorphism
In Calculator class :
public void add(int i, int j)
public void add(int i, double j)
public void add(double i, double j)
These 3 methods hae the same names, but if you can see the datatypes in the parameters are different one is int and other one is double
In MyClass class :
void test(int a)
void test(int a, int b)
double test(double a)
you can see these 3 methods have the same name, but different return types and different number of arguments and different type of data types used in the parameters
Get Answers For Free
Most questions answered within 1 hours.