Search code examples
javagenericsoverloading

Java method overloading based on generics restrictions


Simplified description: I have method process(), which should do different things for objects which implement only First interface and different things for objects which implement First and Second interface at the same time. Naturally, an object implementing both interfaces still match to the process() method which requires implementing only the First interface. But fortunately the most specific overload is called as I need.

My question is, whether it's somewhere defined that this shall work on all Java implementations.

Working sample:

public class Main
{
  public interface Printer
  {
    void print ();
  }

  public interface Calculator
  {
    void calc ();
  }

  public static class MyPrinter implements Printer
  {
    @Override public void print ()
    {
      System.out.println ("MyPrinter");
    }
  }

  public static class MyPrintingCalculator implements Printer, Calculator
  {
    @Override public void print ()
    {
      System.out.println ("MyPrintingCalculator");
    }

     @Override public void calc ()
    {
      System.out.println ("Calculating");
    }
  }

  public static <T extends Calculator & Printer > void process (T something)
  {
    something.print ();
    something.calc ();
  }

  public static void process (Printer something)
  {
    something.print ();
  }

  public static void main (String args[])
  {
    final MyPrinter printer = new MyPrinter ();
    final MyPrintingCalculator calculator = new MyPrintingCalculator ();

    process (printer);
    process (calculator);
  }
}

Try on onlinegdb


Solution

  • TL;DR yes, this behavior is well specified and hence, guaranteed to work across all Java implementations.

    The choice of the most specific method is described in JLS, §15.12.2.5. Choosing the Most Specific Method as

    If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

    The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.

    the latter section being sufficient to explain why <T extends Calculator & Printer> void process(T something) is more specific than process(Printer something). Any valid argument to the former is a subtype of Calculator and Printer, hence, could be passed to process(Printer) without errors (but not the other way round).

    The caveat here is that you have to declare the methods in a specific way to compile the overloaded declarations in the first place. Had you written <T extends Printer & Calculator> void process(T something), the code wouldn’t compile.

    The reason is given in JLS, §4.6. Type Erasure:

    The erasure of a type variable (§4.4) is the erasure of its leftmost bound.

    So while being the same generic type, the erasure of <T extends Calculator & Printer> is Calculator but the erasure of <T extends Printer & Calculator> is Printer.

    Had you used <T extends Printer & Calculator> void process(T something), the erased method declaration would be void process(Printer) and conflict with the non-generic void process(Printer) method.

    The declaration <T extends Calculator & Printer> void process(T something) is guaranteed to produce a method process(Calculator), distinct from process(Printer) after type erasure. For completeness, had you both, a void process(Printer) and a void process(Calculator), you could use this knowledge about type erasure to still create a distinct method for the generic variant, by using <T extends Object & Calculator & Printer>.