0
Can any one explain me how overloading is a compile time polymorphisam?
2 Answers
+ 1
Example:
class A {
public void someMethod() {
}
}
class B extends class A {
public void someMethod() {
/*This method overrides the method
in class A */
}
public void methodOverload(int x, double y) {
//...
}
public void methodOverload(double c) {
//...
}
}
class HomeBase {
public static void main(String[] args) {
B objectB = new B( );
objectB.methodOverload(6, 3.14);
/* The compiler determines the 2-parameter
methodOverload(int x, double y) is the correct choice at compile time. The choice is clear even before objectB
is created. */
objectB.someMethod();
/*The program cannot decide which
someMethod() to use until it creates the objectB at runtime.
*/
}
}
0
Hi,
Method overloading is a compile time polymorphism because the method to be executed is chosen at runtime - based on the arguments.