BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Difference between Static and Dynamic Dispatch in Java

Last Updated: June 2, 2024 by Chaitanya Singh | Filed Under: java

In Java, when we call a method inside a program, the method call is resolved either at the compile time or during runtime, which is known as static and dynamic dispatch respectively.

Static Dispatch (Compile-Time Polymorphism)

  • When a method call is resolved at compile time, it is known as static dispatch. It is also referred as compile time polymorphism.
  • There can be more than one methods with the same name in the class, however these methods have different parameters list. This concept is known as method overloading. In this case also, the method call is resolved at compile time.

Static Dispatch Example

class StaticDispatchExample {
void print(int a) {
System.out.println("Number: " + a);
}

void print(String str) {
System.out.println("String: " + str);
}

public static void main(String[] args) {
StaticDispatchExample example = new StaticDispatchExample();
example.print(10); // Calls print(int a)
example.print("Hello"); // Calls print(String str)
}
}

In this example, the method call example.print(10) and example.print("Hello") are resolved at compile time based on the parameters list. This is because compile is smart enough to judge which method is being called based on the arguments that we passed during method call.

Dynamic Dispatch (Run-Time Polymorphism)

  • When a method call is resolved during runtime, it is known as dynamic dispatch. It is also referred as run time polymorphism.
  • The method call during method overriding is resolved during runtime, this is because the same method is present in both parent and child class and their parameters list is also same. In this case, compiler is not able to resolve the method call during compile time, thus this is resolved during runtime.

Dynamic Dispatch Example

class Parent {
void show() {
System.out.println("Parent show()");
}
}

class Child extends Parent {
@Override
void show() {
System.out.println("Child show()");
}
}

public class DynamicDispatchExample {
public static void main(String[] args) {
Parent obj = new Child();
obj.show(); // Calls Child's show() method at runtime
}
}

In this example, the show() method in the child class is being called because the object (new child()) belongs to child class.

Key Differences between static and dynamic dispatch

  1. Method call resolved at:
    • Static Dispatch: Resolved at compile time.
    • Dynamic Dispatch: Resolved at runtime.
  2. Polymorphism Type:
    • Static Dispatch: Compile-time polymorphism (Method Overloading).
    • Dynamic Dispatch: Run-time polymorphism (Method Overriding).
  3. Flexibility:
    • Static Dispatch: Less flexible, as decisions are made at compile time.
    • Dynamic Dispatch: More flexible, as decisions are made at runtime.
  4. Example:
    • Static Dispatch: Method overloading.
    • Dynamic Dispatch: Method overriding.

Can a developer decide which dispatch to use?

A developer designs the class and methods so they can write the code in such a way, that a particular dispatch is being used. However, they cannot directly choose dispatch mechanism during method calls, as this is determined by the rules of the programming language. If a developer want to influence which dispatch to use, they can do by using following approach:

  • In order to use static dispatch, a developer can use the concept of method overloading in the program and avoid writing the same method (with same parameter list) in parent and child class both.
  • In order to use dynamic dispatch, a developer can use the concept of method overriding.

Similar articles:

  • Static and dynamic binding
  • Compile time and runtime polymorphism
  • Method Overloading with examples
  • Method Overriding with examples

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap