• Register
499 points
8 4 3
Difference between static and dynamic in java

The main difference between Static and Dynamic Polymorphism is that Static Polymorphism is a type of polymorphism that resolves at compile time while Dynamic Polymorphism is a type of polymorphism that resolves at run time.

OOP is a popular software paradigm which allows programmers to model the real world scenarios as objects. Polymorphism is a major pillar of OOP. It refers to the ability of an object to perform in different ways.  Static and dynamic polymorphism are two types of polymorphism. Programmers can implement polymorphism concepts using Java

Key Terms

Dynamic Binding, Dynamic Polymorphism, Early Binding, Java, OOP, Late Binding, Polymorphism, Static Binding, Static Polymorphism

Difference Between Static and Dynamic Polymorphism in Java - Comparison Summary

What is Static Polymorphism in Java

Static polymorphism (or Early Binding) is a type of polymorphism that resolves at compile time. Method overloading is an example of static polymorphism. In method overloading, there are methods with the same name but different parameters. In other words, there are methods with the same name, but they have different data types and a different number of arguments. Moreover, the method to call is determined at compile time. An example program is as follows.

Difference Between Static and Dynamic Polymorphism in Java

Figure 1: Static Polymorphism in Java

There are two methods in the test class with the same name “add”. The first add method accepts two integers and returns the sum of those two numbers. Similarly, the second add method accepts three integers and return the sum of those three numbers. In the main method, there is an object of the type Test class.  Then, the add methods are called on the object. Line 12 calls the add method with two parameters while Line 13 calls add method with three parameters. Likewise, the method to call is determined at the compile time.

What is Dynamic Polymorphism in Java

Dynamic Polymorphism (or Late Binding) is a type of polymorphism that resolves at run time. Method overriding is an example of dynamic polymorphism. In method overriding, there are two classes: one is the parent class while the other is the child class. However, both classes have the same method. Moreover, the method in the child class overrides the method of the parent class. An example is as follows.

Main Difference - Static vs Dynamic Polymorphism in Java

Figure 2: Class A

Static and Dynamic Polymorphism in Java_Figure 3

Figure 3: Class B

Static vs Dynamic Polymorphism_Figure 4

Main class

Class A has a display method and Class B also has a display method. Moreover, Class B extends class A. In the main method, a child class object is assigned to the parent class reference. It determines which method to call at run time.  Then, the display method is called using the object. The programmer can see that the display method of class B executes. In other words, the display method of class A is overridden by the display method of class B.

Difference Between Static and Dynamic Polymorphism in Java

Definition

Static polymorphism is a type of polymorphism that collects the information to call a method during compile time while dynamic polymorphism is a type of polymorphism that collects information to call a method at run time. Thus, this is the main difference between static and dynamic polymorphism.

Synonyms

Synonyms of static polymorphism are static binding and early binding, whereas dynamic binding and late binding are synonyms of dynamic polymorphism.

Occurrence

Static polymorphism occurs at compile time while dynamic polymorphism occurs at run time. Hence, this is another difference between static and dynamic polymorphism.

Execution

Moreover, an important difference between static and dynamic polymorphism is that the execution speed is high in static polymorphism while execution speed is low in dynamic polymorphism.

Example

Method overloading is an example of static binding while method overriding is an example of dynamic binding.

Conclusion

Overall, Polymorphism is a pillar of OOP that describes the ability of an object to behave in multiple ways. In brief, Static and dynamic polymorphism are two types of polymorphism. The main difference between Static and Dynamic Polymorphism is that Static Polymorphism is a type of polymorphism that resolves at compile time while Dynamic Polymorphism is a type of polymorphism that resolves at run time. 

1 Comment

This article is really helpful. Thanks.

More Posts

Difference between static and final keyword in java Sanjana Sagar - May 22
Difference between abstract and interface in java Sanjana Sagar - May 28
Difference between abstract and interface in java Sanjana Sagar - May 28
Difference between class and method in java Sanjana Sagar - May 20
What is the difference between java and core java Sanjana Sagar - May 28
Difference between static and final variable in java Huzaifa-Glitch - May 29
Difference between static and nonstatic methods in java Sanjana Sagar - May 19
What is static binding and dynamic binding offonime - Jun 4
What is the difference between abstraction and encapsulation in java Sanjana Sagar - May 20
Difference between var and dynamic and object in c# mhfaisal - May 7