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

ぃ、小莉子 提交于 2021-01-28 10:14:12

问题


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%

回答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



来源:https://stackoverflow.com/questions/43960454/does-java-support-multiple-dispatch-if-not-how-is-the-below-code-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!