Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Method Overriding in Java
2.1.
Example
2.2.
Java
2.2.1.
Output
2.2.2.
Explanation
3.
What is Dynamic Method Dispatch?
3.1.
Example
3.2.
Java
3.2.1.
Output
3.2.2.
Explanation
4.
Advantages of Dynamic Method Dispatch
5.
Disadvantages of Dynamic Method Dispatch
6.
Frequently Asked Question
6.1.
What do you mean by polymorphism?
6.2.
What do you understand by method overriding?
6.3.
What is the difference between static and dynamic method dispatch in Java?
6.4.
What do you mean by runtime polymorphism?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Dynamic Method Dispatch in Java

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Hello Ninjas, do you know we can define methods with the same name in child classes in Java? Have you ever wondered how to do this? If not, then don’t worry. We will clear all your doubts.

dynamic method dispatch in java

In this article, we will discuss about dynamic method dispatch in Java. We will discuss this concept by using an example. We will also understand its advantages and disadvantages. Dynamic method dispatch is a mechanism of overridden methods that are resolved at run time. This is also known as runtime polymorphism. Before diving deeper into this topic, let us understand a very important concept of method overriding. We will also understand its usefulness in dynamic method dispatch in Java.

Method Overriding in Java

Method overriding is one of the important concepts of Object-oriented programming. It helps subclasses to provide different implementations of a method with the same name and parameters as like parent class. This method should have the same return type as well. Let us understand this with the help of an example.

method overriding in java

Example

Here is an example to understand the method overriding in Java.

  • Java

Java

class CodingNinjas {
   // Creating a ninjas() method
   public void ninjas() {
       System.out.println("This is for entire company.");
   }
}

// Ninjas class that extends CodingNinjas
class Ninjas extends CodingNinjas {
   public void ninjas() {
       System.out.println("This is for a Ninja.");
   }
}
// CnEmployee class that extends CodingNinjas
class CnEmployee extends CodingNinjas {
   public void ninjas() {
       System.out.println("This is for a employee in Coding Ninjas.");
   }
}
class Main{
   public static void main(String args[]){
       // Creating object for class CodingNinjas
       CodingNinjas obj=new CodingNinjas();
       obj.ninjas();
       // Creating object for class Ninjas
       Ninjas obj1=new Ninjas();
       obj1.ninjas();
       // Creating object for class CnEmployee
       CnEmployee obj2=new CnEmployee();
       obj2.ninjas();
   }

   

Output

This is for entire company.
This is for a Ninja.
This is for a employee in Coding Ninjas.

Explanation

In this example, we have a superclass called CodingNinjas and two subclasses, Ninjas and CnEmployee. Each subclass overrides the ninjas() method of the superclass and provides its own implementation. Then we created objects for each class in the main to access the ninjas() method of each class.

Now as we have created objects for each class, we can do the same thing in a different way. Let us understand this.

Also see, Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Dynamic Method Dispatch?

Dynamic method dispatch in Java is a mechanism that helps to call an overridden method at runtime by creating referenced objects. It is the same as runtime polymorphism. Dynamic method dispatch in Java uses upcasting. Upcasting is a process to convert the reference variable of the child class to the reference variable of the parent class. Let us understand this concept with the help of an example. 

Example

Here is an example to understand dynamic method dispatch in Java.

  • Java

Java

class CompanyOfCars {
   // Creating a company() method
   public void company() {
       System.out.println("Welcome to our car gallery.");
   }
}

// Bmw class that extends CompanyOfCars
class Bmw extends CompanyOfCars {
   public void company() {
       System.out.println("BMW is a great company.");
   }
}
// Mercedes class that extends CompanyOfCars
class Mercedes extends CompanyOfCars {
   public void company() {
       System.out.println("Mercedes gives you royalty.");
   }
}
class Main{
   public static void main(String args[]){
       // Creating object for class CompanyOfCars
       CompanyOfCars obj=new CompanyOfCars();
       obj.company();
       // Creating object for class Bmw
       obj=new Bmw();
       obj.company();
       // Creating object for class Mercedes
       obj=new Mercedes();
       obj.company();
   }
}

Output

Welcome to our car gallery.
BMW is a great company.
Mercedes gives you royalty.

Explanation

In this example, we have created a superclass CompanyOfCars. This class extends two subclasses, Bmw and Mercedes. These two subclasses override a method company(). In this example, we have created objects of these subclasses differently if we compare them with the method overriding’s example. This mechanism of calling the overridden methods at runtime is called dynamic method dispatch in Java.

As mentioned above in the example, the method call is connected to the method body. This process is known as binding. There are two types of binding that are possible:

1. Static Binding: In this type of binding, the type of the object is determined at the time of compilation.

2. Dynamic Binding: In this type of binding, the type of the object is determined at the runtime.

Advantages of Dynamic Method Dispatch

There are various advantages of dynamic method dispatch in Java:

  • This allows for polymorphism. That’s why we can write more flexible and maintainable code that can be extended and reused.
     
  • It helps us to reuse the code because of method overriding.
     
  • It makes our code more flexible. We can change the behavior of the subclass method without changing the behavior of the parent class method.
     
  • It allows us to define abstract methods in a superclass and implement them in subclasses. 

Disadvantages of Dynamic Method Dispatch

Along with some advantages, dynamic method dispatch in Java also has some disadvantages:

  • It adds some performance overhead to method calls. The JVM(Java Virtual Machine) has to determine the actual class of an object at runtime before it can call the appropriate method. 
     
  • It can add complexity to code. Developers must understand the relationships between classes and how inheritance and method overriding work. 
     
  • It can cause unintended consequences if a subclass overrides a method in a superclass that was not intended to be overridden. 
     
  • It can also introduce security issues if malicious code can access objects of unexpected types or override methods in unexpected ways.

Frequently Asked Question

What do you mean by polymorphism?

Polymorphism is an one of the important component of OOPs. It refers to the ability of objects of different classes to be treated as if they are objects of the same class. 

What do you understand by method overriding?

Method overriding helps a subclass to provide its own implementation of a method that is already defined in its superclass(parent class). The method should have the same name, return type, and parameters as the method defined in the superclass.

What is the difference between static and dynamic method dispatch in Java?

In Java, Static method dispatch occurs during compile time and is based on the reference type. On the other hand, Dynamic method dispatch happens at runtime and depends on the actual object's type, allowing for method overriding and polymorphism.

What do you mean by runtime polymorphism?

Runtime polymorphism is also known as late binding. It allows a program to determine which method to invoke at runtime rather than at compile-time.

Conclusion

In this article, we have discussed the concept of dynamic method dispatch in Java. We have also discussed some examples based on this concept. We have also covered its advantages and disadvantages. You can check out our other blogs to enhance your knowledge:

We hope this blog helped you to understand the dynamic method dispatch in Java. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews

Happy Learning!!

Previous article
Method Overriding In Java
Next article
Can we Overload or Override static methods in java?
Live masterclass