CBSE Class 11 » Difference Between » Compile Time and Run Time Polymorphism in Java

Compile Time and Run Time Polymorphism in Java

Are you interested in learning Java compile time and run time polymorphism? If yes, then this guide will provide you with all the information related to compile time and run time polymorphism in java. After going through this article, there will be no confusion between the two.

Polymorphism in Java

Java has different features in it. They are inheritance, polymorphism, data abstraction, encapsulation, etc. As we can see, polymorphism is one of them. 

Polymorphism is made up of two Greek words, “poly” and “morphs”. “poly” means many and “morphs” means forms. Therefore, polymorphism means the ability to exist in different forms. A real-life example of polymorphism is that one word can be used differently. 

Consider the word spring. In one form, it is a season, and in the other form, it means a metal coil. Consider the other example, well. On the one hand, well depicts positivity, and on the other hand, it means a structure dug in the ground for storing water.

In java, polymorphism means the ability to perform the same task differently. For example, we can use inheritance when many classes are linked by inheritance. Inheritance is the property of an Object-Oriented Programming Language in java that helps create a new class from an existing class. By doing this, the new class created can inherit the attributes and methods from the existing class. Now, polymorphism uses these attributes and methods to perform different tasks. 

In java, polymorphism is of two types. These are compiled time and run time polymorphisms.

Compile Time Polymorphism

It is also known by the terms static polymorphism, early binding, or overloading. Finally, compile-time polymorphism is a process in which the method is called compile time. 

Method Overloading is an act of implementing compile-time polymorphism. Other examples of compile-time polymorphism are Constructor Overloading and Method Hiding.

Method Overloading

Method Overloading is a process for implementing compile-time polymorphism in which the methods have the same name but possess different parameters. In this way, the same command will be displayed in different ways. 

Different ways to carry out Method Overloading are:

  • First, by changing the number of parameters: The class name remains the same in this type of overloading, but the parameters taken as inputs are varied
  • By changing the data types of parameters: In this type of overloading, the class name is kept the same, but the data types of the parameters taken as input are varied

Runtime Polymorphism

It is also known as dynamic polymorphism, late binding, or overriding. In this type of polymorphism, the method is called during the run time. Method Overriding is used for the implementation of run time polymorphism. In this, a child class inherits the data of the parent class. Both classes possess the same methods, but implementation is different. 

Difference Between Compile Time and Run Time Polymorphism

Compile Time Polymorphism

Runtime Polymorphism

  • It is also known as static polymorphism, early binding, or overloading
  • It is also known as dynamic polymorphism, late binding, or overriding
  • It is executed at the compile time
  • It is executed at the run time
  • The method is called with the help of a compiler
  • The method is not called with the help of a compiler
  • The program’s execution is fast as it involves the use of a compiler
  • The program’s execution is slow as it does not involve a compiler
  • It is achieved by function overloading and operator overloading
  • It is achieved by virtual overloading and pointers
  • Compile-time polymorphism tends to be less flexible as all commands are operated at the compile time
  • Example: Method Overloading
  • Run time polymorphism tends to be more flexible as all commands are executed at the run time
  • Example: Method Overriding

Conclusion

This article first provides a basic explanation for the polymorphism and then explains the compile-time and runtime polymorphism and then the difference between the two. 

By going through this article, one can easily explain the key differences between the compile-time and runtime polymorphism. 

faq

Frequently asked questions

Get answers to the most common queries related to the Programming Examination Preparation.

Q1. What is OOP?

Answer : OOP stands for Object-Oriented Programming Language. It is a way of programming that uses objects and class...Read full

Q2. What is inheritance?

Answer : Inheritance is the property of an Object-Oriented Programming Language in java that helps create a new clas...Read full

Q3. What is encapsulation?

Answer : Encapsulation is the process of binding the data together in a single unit. In a way, encapsulation hides t...Read full

Q4. What is data abstraction?

Answer : Data abstraction is an important feature of Object-Oriented Programming Language. It is the feature that di...Read full