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




