Posted By:
java_pro
Posted On:
Tuesday, November 2, 2004 04:02 AM
The compile time polymorphism is function overloading and run time polymorphism is function overriding
overloading example :
class base { void fun(int a,int b) { System.out.println("hai"); }
void fun(int a) { System.out.println("hello"); }
}
public class Overriding { public static void main(String str[]) {
base b = new base(); b.fun(1); b.fun(1,2);
} }
In the case of overloading, the compiler comes to know at the compile time which function has to be called(b.fun(1),b.fun(1,2)).
overriding example :
class base { void fun() { System.out.println("base"); } }
class derived extends base { public void fun() { System.out.println("derived"); } }
public class Overriding { public static void main(String str[]) { derived d = new derived(); base b = d; d.fun(); } }
In the case of overriding the call to the function d.fun() is defered till run time. i.e during compile time the compiler does not know whether to call base class fun() or derived class fun().