Difference between compile time (static binding) and run time polymorphism (dynamic binding) in java ?
Difference between compile time (static binding) and run time polymorphism (dynamic binding) in java ?
Polymorphism refers to the existence of many different forms. Polymorphism can be simply defined as a message’s capacity to be presented in multiple forms. We have provided the main comparisons or differences between compile-time and run-time polymorphisms as given below.
Compile-time or build-time polymorphism: Compile-time or Build-time polymorphism is the process of binding an object’s functionality at the time of build. Java determines the method to call at compile time by examining the method signatures. Compile-time polymorphism, static polymorphism, or early binding are all terms for this. Method overloading is used to achieve compile-time polymorphism. According to method overloading, a class may contain many functions with the same name but different prototypes. One technique to create polymorphism is through function overloading, however this depends on the technology used and the kind of polymorphism chosen. Function overloading is accomplished in Java at compile time. The below given is the example of compile- time polymorphism in java
public class AddOneAndTwo { // Sum of One and Two provides int values public static int add(int one, int two) { return one + two; } // Sum of One and Two provides double values public static double add( double one, double two) { return one + two; } // Driver code public static void main(String args[]) { System.out.println(add(1, 2)); System.out.println(add(1.1, 2.2)); } }
Runtime polymorphism: Runtime polymorphism is the process of binding an object to functionality during runtime. Method overriding can be used to achieve runtime polymorphism. The appropriate method to invoke is decided by the Java virtual machine at runtime rather than during compilation. It is also known as late binding or dynamic binding. The parent class’s method is overridden in the child class, according to the concept of method overriding. This indicates that method overriding occurs when a child class provides a specific implementation of a method that was given by one of its parent classes. The below given is the example of run-time polymorphism in java
class PrintClassOne { public void print() { System.out.println("Invokes Print from the parent class "); } } public class PrintClassTwo extends Test { public void print() { System.out.println("Invokes Print from another class"); } public static void main(String args[]) { PrintClassOne classOne = new PrintClassTwo(); classOne.method(); } }
Difference between Compile time and Run time polymorphism in java
Compile Time polymorphism or Static Binding in Java | Run Time polymorphism or Dynamic Binding in Java |
The bindings which takes place at the compile time are called Static binding in java | The bindings which takes place at run time are called Dynamic binding in java |
Compile-time polymorphism is also called as Early Binding or Static binding | Run-time polymorphism is also called as Late Binding or Dynamic binding |
The method definition and method call are linked during the compile time | The method definition and method call are determined at run time |
Binding of all the static, private and final methods is done at compile-time. Method overloading is an example of Compile-time polymorphism | Method overriding is an example of Run-time polymorphism where method call is determined at run time. |
Program execution is faster as objects are determined at compile time | Program execution is slower as objects are determined at run time |
Compile-time polymorphism provides less flexibility | Run-time polymorphism provides more flexibility |
Leave a Reply