• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Complie time polymorphism vs Runtime polymorphism

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rathi........

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

 
Krish Pinnamaneni
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic