aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Complie time polymorphism  vs Runtime polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Complie time polymorphism  vs Runtime polymorphism" Watch "Complie time polymorphism  vs Runtime polymorphism" New topic
Author

Complie time polymorphism vs Runtime polymorphism

Krish Pinnamaneni
Greenhorn

Joined: Mar 16, 2005
Posts: 22
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Mar 05, 2005
Posts: 206
hi rathi........

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

Krish Pinnamaneni
Greenhorn

Joined: Mar 16, 2005
Posts: 22
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Complie time polymorphism vs Runtime polymorphism
 
Similar Threads
overloading and overriding question
Polymorphism-overloading/overriding
Interesting classpath question
Jaworski mock - an example of polymorphism (overriding or overloading?)
What is the logic behind designing how Overloaded vs Overridden methods are invoked?