Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Complie time polymorphism vs Runtime polymorphism

 
Krish Pinnamaneni
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need detailed explanation of difference between compile time polymorphism and run time polymorphism?. How complie time polymorphism and run time polymorphism is achieved. Can any body explain with detailed example.

In advance,
Thanks,
Krish
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no anology like compile time polymorphism, polymorphism only happen run time. Try this:
1] create a class, declare one method in it.
2] create another class that extends previous one and override that method.
3] write main method in second class, and in that, assign sub class object to super class reference.
4] call that method.

what happens, at compile time, it is binded to reference and at run time it is binded to object and thatswhy the overriding method gets execute.
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rathi........

long time.........ha

 
Krish Pinnamaneni
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rathi Ji,

Thanks for spending your most valuable time fo giving response.

I tried the following example:
class Base {
public void test(){
System.out.println("Super class test()");

}

}

class Sub extends Base {
public void test(){
System.out.println("test() is overriding method");
}

public static void main(String[] args){

Base ob = new Sub();
ob.test();
}

}

//output is: test() is overriding method
But my doubt is: deciding which method to be called during compile time is compile time polymorphism.
ex: Sub ob = new Sub(); //This is example for compile time polymorphism.

Deciding which method to be called at runtime is runtime polymorphism.
Super ob = new Sub();//Example for runtime polymorphism.Thanks for spending your most valuable time for giving response.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic