1

Does java support multiple dispatch? If not how is the below code working?

Account.java

public interface Account 
{
    public void calculateInterest();
}

SavingsAccount.java

public class SavingsAccount implements Account
{

}

LoanAccount.java

public class LoanAccount implements Account
{

}

InterestCalculation.java

public class InterestCalculation 
{
    public void getInterestRate(Account objAccount)
    {
        System.out.println("Interest Rate Calculation for Accounts");
    }

    public void getInterestRate(LoanAccount loanAccount)
    {
        System.out.println("Interest rate for Loan Account is 11.5%");
    }

    public void getInterestRate(SavingsAccount savingAccount)
    {
        System.out.println("Interest rate for Savings Account is 6.5%");
    }
}

CalculateInterest.java

public class CalculateInterest 
{
     public static void main(String[] args) 
     {
         InterestCalculation objIntCal = new InterestCalculation();
         objIntCal.getInterestRate(new LoanAccount());
         objIntCal.getInterestRate(new SavingsAccount());        
     }
}

Output

Interest rate for Loan Account is 11.5%
Interest rate for Savings Account is 6.5%
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Java Beginner
  • 1,635
  • 11
  • 29
  • 51
  • 1
    This is just how method overloading works. Your actual method parameters are specific subtypes, not merely `Account`. – Andy Turner May 14 '17 at 04:55
  • You are telling method overloading and multiple dispatch are same? – Java Beginner May 14 '17 at 04:58
  • new LoanAccount() and new SavingsAccount() are created at RunTime, Then how comes it is overloading? – Java Beginner May 14 '17 at 05:02
  • 1
    try passing a variable of type `Account` , (assuming later at run time you will instansiate it with concrete object reference). In this case your method corresponding to Account as parameter will be invoked. It is simple method overloading you are having. When compiler sees same method name being invoked then it sees what is the argument you are passing in it and then it decides which method has matching parameters for those arguments. – nits.kk May 14 '17 at 05:29

1 Answers1

1

First some terminology

Overloading is the ability to create multiple methods of the same name with different implementations.

In Java, this is performed based on the compile-time type of the arguments, : the method with the matching signature is used, independently of the argument's value.

Overriding allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

In Java, this is performed by determining the method to be called depending on the run-time (dynamic) type of the object referred to. This is Java's way to implement single dispatch and it is not be confused with overloading.

Multiple dispatch means that a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case some other attribute, of more than one of its arguments.

Does Java support multiple dispatch ?

In Java, there's no direct support for multiple dispatch.

However you can emulate multiple dispatch by using several layers of single dispatch combined with overloading.

How is this code working ?

Note that this code to work first requires that you provide an implementation of calculateInterest() for LoanAccount and SavingAccount, even if it will not be used in the rest of your POC.

In your class InterestCalculation , you have three distinct overloads of the method getInterestRate(). They each have a distinct signature (e.g. argument type). So main() will invoke the right method depending on the declared type of the argument (which is deduced from the constructor).

You could optimize your use of single dispatch and make InterestCalculation much more general (e.g. you could then add many more implementations of Account without having to change it) :

public interface Account 
{
    public void calculateInterest();
    public double getInterest(); 
    public String getType(); 
}
...
public class InterestCalculation 
{
    public void getInterestRate(Account objAccount)
    {
        System.out.print ("Interest Rate for "+objAccount.getType()+" is ");
        System.out.print (objAccount.getInterest());
        System.out.println (" %");
    }
}

Online demo

Community
  • 1
  • 1
Christophe
  • 68,716
  • 7
  • 72
  • 138