Signup/Sign In

Difference Between Runtime Polymorphism and Compile time Polymorphism

Introduction

Polymorphism is one of the most significant ideas in Java since it allows us to do the same operation in several ways. There are two forms of polymorphism: compile time polymorphism (Static) and dynamic runtime polymorphism (Dynamic). Let's look at how Compile-time and Run-time Polymorphism vary in Java.

Compile Time Polymorphism

At build time, the compiler checks the method signature to identify which method to call for a particular method call. Compile-Time Polymorphism is named after the fact that it occurs during the compilation process. It's also known as early binding, static polymorphism, or overloading.

One technique to achieve compile-time polymorphism in Java is to use method overloading. This polymorphism also includes function Object() { [native code] } overloading and method hiding.

Run-Time Polymorphism

A polymorphism that gathers information in order to invoke a method at runtime. It's sometimes referred to as dynamic binding, late binding, or overriding. It's also known as run-time polymorphism since it occurs in real time. At runtime, the Java Virtual Machine (JVM) decides which method call will be called with the method body.

To achieve dynamic or runtime polymorphism in Java, method overriding is employed. To provide a more specialised implementation, a derived or child class overrides a base or parent class method. The only difference between the parent and child classes is how the method is implemented.

Let's take a look at upcasting before going on:

When a reference variable in the Parent class refers to an object in the Child class, this is known as upcasting. This is how we resolve overridden methods in Java at runtime.

Comparison Table Between Runtime and Compile time Polymorphism

Compile-time Polymorphism Runtime Polymorphism
  • The compiler resolves the call in Compile Time Polymorphism.
  • The compiler does not resolve calls in Run-time Polymorphism.
  • Static binding, Early binding, and overloading are all terms used to describe this phenomenon.
  • Dynamic binding, Late binding, and overriding are all terms used to describe this process.
  • Overloading is a sort of compile-time polymorphism in which many methods with the same name have distinct arguments, signatures, and return types.
  • Overriding is a kind of polymorphism that occurs at runtime and involves the use of the same method with the same arguments or signature in a class and its subclass.
  • Function overloading and operator overloading are used to accomplish this.
  • Virtual functions and pointers are used to accomplish this.
  • Because it is known early in the compilation process, it allows for quick execution.
  • Because it is known at runtime, it offers slower execution than early binding.
  • Because everything happens at build time, compile time polymorphism is less versatile.
  • Because everything happens at run time, run time polymorphism is more versatile.

Conclusion

One of the most important components of Object-Oriented Programming is polymorphism. By the conclusion of this essay, we should have a good understanding of the two types of polymorphism in Java: compile-time and run-time polymorphism. Method binding and method invocation are handled differently by compile-time and run-time polymorphism.



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.