Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I require a method argument in Java to implement multiple interfaces?

It's legal to do this in Java:

 void spew(Appendable x)
 {
     x.append("Bleah!\n");
 }

How can I do this (syntax not legal):

 void spew(Appendable & Closeable x)
 {
     x.append("Bleah!\n");
     if (timeToClose())
         x.close();
 }

I would like if possible to force callers to use objects that are both Appendable and Closeable, without requiring a specific type. There are multiple standard classes that do this, e.g. BufferedWriter, PrintStream, etc.

If I define my own interface

 interface AppendableAndCloseable extends Appendable, Closeable {}

that won't work since the standard classes that implement Appendable and Closeable do not implement my interface AppendableAndCloseable (unless I don't understand Java as well as I think I do... empty interfaces still add uniqueness above and beyond their superinterfaces).

The closest I can think of is to do one of the following:

  1. pick one interface (e.g. Appendable), and use runtime tests to ensure the argument is an instanceof the others. Downside: problem not caught at compile time.

  2. require multiple arguments (catches compile-time correctness but looks dorky):

    void spew(Appendable xAppend, Closeable xClose)
    {
        xAppend.append("Bleah!\n");
        if (timeToClose())
            xClose.close();
    }
    
like image 744
Jason S Avatar asked Sep 30 '09 21:09

Jason S


People also ask

Can we implement multiple interfaces with same method in Java?

No, its an errorIf two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.

Can a method implement multiple interfaces?

Yes, a class can implement multiple interfaces. Each interface provides contract for some sort of behavior.

How can we implement multiple interfaces in Java?

Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

Can Java interfaces be passed as method arguments?

Yes, you can pass Interface as a parameter in the function.


1 Answers

You could do it with generics:

public <T extends Appendable & Closeable> void spew(T t){
    t.append("Bleah!\n");
    if (timeToClose())
        t.close();
}

Your syntax was almost right, actually.

like image 94
Michael Myers Avatar answered Oct 17 '22 23:10

Michael Myers