Open In App

Difference between Compile-time and Run-time Polymorphism in Java

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. In this article, we will see the difference between two types of polymorphisms, compile time and run time. 

Compile Time Polymorphism: Whenever an object is bound with its functionality at the compile time, this is known as the compile-time polymorphism. At compile-time, java knows which method to call by checking the method signatures. So this is called compile-time polymorphism or static or early binding. Compile-time polymorphism is achieved through method overloading. Method Overloading says you can have more than one function with the same name in one class having a different prototype. Function overloading is one of the ways to achieve polymorphism but it depends on technology and which type of polymorphism we adopt. In java, we achieve function overloading at compile-Time. The following is an example where compile-time polymorphism can be observed. 

Java
// Java program to demonstrate
// compile-time polymorphism
public class GFG {

    // First addition function
    public static int add(int a, int b)
    {
        return a + b;
    }

    // Second addition function
    public static double add(
        double a, double b)
    {
        return a + b;
    }

    // Driver code
    public static void main(String args[])
    {
        // Here, the first addition
        // function is called
        System.out.println(add(2, 3));

        // Here, the second addition
        // function is called
        System.out.println(add(2.0, 3.0));
    }
}

Output
5
5.0

Run-Time Polymorphism: Whenever an object is bound with the functionality at run time, this is known as runtime polymorphism. The runtime polymorphism can be achieved by method overriding. Java virtual machine determines the proper method to call at the runtime, not at the compile time. It is also called dynamic or late binding. Method overriding says the child class has the same method as declared in the parent class. It means if the child class provides the specific implementation of the method that has been provided by one of its parent classes then it is known as method overriding. The following is an example where runtime polymorphism can be observed.

Java
// Java program to demonstrate
// runtime polymorphism

// Implementing a class
class Test {

    // Implementing a method
    public void method()
    {
        System.out.println("Method 1");
    }
}

// Defining a child class
public class GFG extends Test {

    // Overriding the parent method
    public void method()
    {
        System.out.println("Method 2");
    }

    // Driver code
    public static void main(String args[])
    {
        Test test = new GFG();

        test.method();
    }
}

Output
Method 2

The following table demonstrates the difference between runtime polymorphism and compile-time polymorphism:

Compile Time PolymorphismRun time Polymorphism
In Compile time Polymorphism, the call is resolved by the compiler.In Run time Polymorphism, the call is not resolved by the compiler.
It is also known as Static binding, Early binding and overloading as well.It is also known as Dynamic binding, Late binding and overriding as well.
It is achieved by method overloadingIt is achieved by virtual functions and pointers.
It provides fast execution because the method that needs to be executed is known early at the compile time.It provides slow execution as compare to early binding because the method that needs to be executed is known at the runtime.
Compile time polymorphism is less flexible as all things execute at compile time.Run time polymorphism is more flexible as all things execute at run time.
Inheritance is not involved. Inheritance is involved.


Previous Article
Next Article

Similar Reads

Compile Time Polymorphism in Java
Polymorphism in Java refers to an object's capacity to take several forms. Polymorphism allows us to perform the same action in multiple ways in Java. Polymorphism is divided into two types: Compile-time polymorphismRun time polymorphismNote: Run time polymorphism is implemented through Method overriding. Whereas Compile Time polymorphism is implem
7 min read
Compile and Run Java Programs in Sublime Text in Linux
Sublime Text is a free minimalist coding editor developed by Sublime HQ for desktop use. This development and IT program enable you to solely focus on your code, leaving all the other types of eye-candy out. Procedure: Open terminal and the specific command are entered there in order to check for the software is there or not. Existence will be disp
2 min read
Difference between Compile Time and Execution Time address binding
Prerequisite - Address Binding Methods Address Binding is the association of program instructions and data to the actual physical memory location. There are various types of address binding in the operating system. There are 3 types of Address Binding: Compile Time Address Binding Load Time Address Binding Execution Time Address Binding Here, we co
2 min read
Difference Between Compile Time and Load Time Address Binding
Address binding is a situation whereby one address is linked to another in computer systems. This mapping can also happen at different times during program execution they may be compile-time, load-time, or run-time Knowing the differences between these types of address binding is important to comprehend the process of memory management and program
4 min read
Difference between runtime exception and compile time exception in PHP
The term PHP is an acronym for Hypertext Preprocessor, which is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension “.php”. It is an interpreted language and it does not require a compiler. PHP code
3 min read
Difference between Compile Time Errors and Runtime Errors
Compile-Time Errors: Errors that occur when you violate the rules of writing syntax are known as Compile-Time errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by the compiler and thus are known as compile-time errors. Most frequent Compile-Time errors are: Missing Pare
3 min read
Difference between link and compile in AngularJS
In this article, we will see the link and compile features in Angular JS, along with understanding their implementation through the illustration and exploring the key differences between them. One of the fundamental components of AngularJS is the directive. When creating web components, a directive enables declarative HTML vocabulary expansion. Att
5 min read
Difference between Inheritance and Polymorphism
Inheritance is one in which a new class is created that inherits the properties of the already exist class. It supports the concept of code reusability and reduces the length of the code in object-oriented programming. Types of Inheritance are:Single inheritanceMulti-level inheritanceMultiple inheritancesHybrid inheritanceHierarchical inheritanceEx
5 min read
While loop with Compile time constants
While loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. It is mostly used in situations where the exact number of iterations beforehand. Below is the image to illustrate the while loop: Syntax: while(test_expression){ // state
6 min read
Interfaces and Polymorphism in Java
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JA is that Java tries to connect every concept in the language to the real world with the help
6 min read
Variables in Java Do Not Follow Polymorphism and Overriding
Variables in Java do not follow polymorphism. Overriding is only applicable to methods but not to variables. In Java, if the child and parent class both have a variable with the same name, Child class's variable hides the parent class's variable, even if their types are different. This concept is known as Variable Hiding. In the case of method over
2 min read
Pattern compile(String) method in Java with Examples
The compile(String) method of the Pattern class in Java is used to create a pattern from the regular expression passed as parameter to method. Whenever you need to match a text against a regular expression pattern more than one time, create a Pattern instance using the Pattern.compile() method.Syntax: public static Pattern compile(String regex) Par
2 min read
Pattern compile(String,int) method in Java with Examples
The compile(String, int) method of the Pattern class used to create a pattern from the regular expression with the help of flags where both expression and flags are passed as parameters to the method. The Pattern class contains a list of flags (int constants) that can be helpful to make the Pattern matching behave in certain ways. For example, The
2 min read
Output of Java program | Set 25 (Polymorphism)
Pre-requisite: Polymorphism in java 1) What is the output of the following program? class GFG { protected void getData() { System.out.println("Inside GFG"); } } class GeeksforGeeks extends GFG { protected void getData() { System.out.println("Inside GeeksforGeeks"); } } public class Test { public static void main(String[] args) {
3 min read
Dynamic Method Dispatch or Runtime Polymorphism in Java
Prerequisite: Overriding in java, Inheritance Method overriding is one of the ways in which Java supports Runtime Polymorphism. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called through a superclass reference, Java determines which
5 min read
Polymorphism in Java
The word 'polymorphism' means 'having many forms'. In simple words, we can define Java Polymorphism as the ability of a message to be displayed in more than one form. In this article, we will learn what is polymorphism and its type. Real-life Illustration of Polymorphism in Java: A person can have different characteristics at the same time. Like a
6 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Difference between Thread.start() and Thread.run() in Java
In Java's multi-threading concept, start() and run() are the two most important methods. Below are some of the differences between the Thread.start() and Thread.run() methods: New Thread creation: When a program calls the start() method, a new thread is created and then the run() method is executed. But if we directly call the run() method then no
3 min read
Compile our own Android Kernel in 5 Simple Steps
The Android kernel helps the applications to communicate with the hardware components of the device. For example: Most of us are familiar with game mode. What it does is instructs the processor and the graphics processing unit to run at their maximum frequencies. Another example is power saver mode. It instructs the processor and the graphics proce
3 min read
Understanding Encapsulation, Inheritance, Polymorphism, Abstraction in OOPs
As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of t
6 min read
Run-Time Stack Mechanism in Java [Use the updated images]
Prerequisite: Exceptions in Java For every thread, JVM (Java virtual machine) creates a run-time stack. Each and every call performed in a thread is stored in the stack.Each entry in the run-time stack is known as an activation record or stack frame.After completing every method call by the thread is removed from the corresponding entry of the stac
3 min read
Why is Java 'write once and run anywhere'?
JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in Java code. JVM is a part of the JRE(Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one system and can expect it to run o
2 min read
Difference between Time Tracking and Time and Attendance Software
Time tracking and time and attendance software are tools that help businesses track the time that employees spend on tasks and their attendance records. These tools can help businesses automate their time tracking processes, reduce errors and inaccuracies, and ensure that employees are paid accurately for the time they work. Time tracking software
4 min read
How to run Java RMI Application
Prerequisite: RMI RMI (Remote Method Invocation) is used for distributed object references system. A distributed object is an object which publishes its interface on other machines. A Remote Object is a distributed object whose state is encapsulated. Stub and Skeleton are two objects used to communicate with the remote object. Stub: Stub is a gatew
4 min read
Different Ways to Run Applet in Java
Applets are tiny Java programs that can be installed and run automatically as part of a web page. They can be viewed on an Internet server, transmitted over the Internet, and installed and run automatically as part of a web document or desktop application. Note:Java applet is deprecated, which means it's no longer recommended for use and may be rem
3 min read
run() Method in Java Thread
The run() method is available in the thread class constructed using a separate Runnable object. Otherwise, this method does nothing and returns. We can call the run() method multiple times. The run() method can be called in two ways which are as follows: Using the start() method.Using the run() method itself. Syntax: public void run() { //statement
3 min read
How to Run Gecko Driver in Selenium Using Java?
Selenium is a well-known software used for software testing purposes. Selenium is consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver, online website testing can be done. There are three main web drivers present. For th
3 min read
How to Run Opera Driver in Selenium Using Java?
Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. There are three main Webdrivers present. For the Chr
3 min read
How to Run Java Program?
Java is a popular, high-level, object-oriented programming language that was developed by James Gosling and his team at Sun Microsystems (now owned by Oracle Corporation) in the mid-1990s. It is widely used for developing various kinds of software, including web applications, desktop applications, mobile applications, games, and more. Step by Step
2 min read
How to Run the Java Code in Git Bash?
Git Bash is a command-line interface that provides a Unix-like environment on Windows. You can use it to compile and run Java programs just as you would in traditional terminals on Linux or macOS. Step to Run Java Code in Git Bash:1. Install JavaMake sure you have Java installed on your Windows computer. You can download and install Java from the o
2 min read
Practice Tags :
three90RightbarBannerImg