What is the difference between Static and Dynamic Polymorphisim?

Questions by gmalcomfernandez

Showing Answers 1 - 24 of 24 Answers

method overloading would be an example of static polymorphism
whereas overriding would be an example of dynamic polymorphism.

B'Coz,in case of overloading,at compile time the compiler knows which method to link to the call .
However,it is determined at runtime for dynamic polymorphism

method overloading is an example of compile time/static polymorphism because method binding b/w method call and method defination happens at compile time and it's depend on the ref. of the class(ref. create at copile time and goes to stack).

method overriding is an example of run time/dynamic polymorphism because method binding b/w method call and method defination happens at run time and it's depend on the object of the class(object create at time and goes to heap).
  

  Was this answer useful?  Yes

sampra

  • Feb 14th, 2008
 

static polymor is compile time polymorphism ie method overloading
dyanmic polymorphism is rumtime polymorphism ie method overriding+dynamic dispatch

  Was this answer useful?  Yes

Static polymarphism is compile time polymarphis, i.e if we take method overloading, at copile time itself method call will bound to method definition. Dynamic polymarphism is runtime polymarphism, i.e if we take method overriding, method call bound to method def at runtime depends on object reference

Polymorphism means existing in various forms.

1).Method Overloading: For example, Same method name and different parameters. Here the method is existing in various forms.

2).Method Overriding: You get a method in your class from it's parent class and you override it(Add your own fucntionality to it). Here also you are using same method name for two diffrent purposes.

Here, however tho JVM at runtime calls the parent class method if you don't add your own fucntionalty(don't override the parent class method). But if you do override the parent class methods, JVM invokes the child class's method. Since this is done at runtime, it's called as runtime polymorphism or dynamic polymorphism.

There is no such decision making in method overloading so it is called as static polymorphism or compile time polymorphism.

Thx.

Static polymorphism is code refinement and where as runtime polymorphism is code replacement.


Static polymorphism is achieved by method overloading and runtime polymorphism is achieved by method overriding.


To implement static polymorphism inheritance is not necessary but to implement runtime polymorphism inheritance is necessary.


Generally when programmer needs to extend existing feature available in software, method overloading is used but a programmer uses method overriding when he wants to provide a different implementation for the same feature

The term static polymorphism is associated with overloaded methods because it gives the impression that a single named method will accept a number of different argument types. The System.out.println() method is an example that may take String or Object references, boolean and other primitive types as an argument. In fact, each overloaded method is separate and the compiler can see the difference between them. In this case, the argument types are fixed at compile time and are considered static. This has nothing to do with the Java keyword static.

Dynamic polymorphism is where a class overrides a superclass method or implements an interface. For example, any class may override the Object.toString() method and provide its own implementation, and this is known at compile time. However, for a simple Java program that instantiates a series of objects and calls their toString() method, the compiler does not consider the object references differently. Any differences in the objects' toString() implementations are only seen at runtime, so they are considered dynamic.

First of all I am telling about Polymorphism


Polymorphism: The term polymorphism was defined from the greek 'Poly' and 'morphos'
which mean 'many' and 'forms'.
Poly+Morphos=Many forms
Polymorphism allows one interface to be used for multiple functions.
Static Polymorphism: In static polymorphism, the response to a function is
decided at compile time
Dynamic Polymorphism: In dynamic polymorphism, the response to the function is
decided at run time


Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions