4

I'm writing a Java application, and have a question. How can I implement a function with one parameter which should require an argument which implements multiple interfaces? For example:

interface Father
{
}

interface Teacher
{
}

public void foo(FatherAndTeacher person) // How do I do this?
{
    // Do something
}

I know I can use two parameters, such as:

public void foo(Father person1, Teacher person2)
{
    // Do something
}

but I think maybe having a single parameter which implements both interfaces would be better.

4
  • I think you'll need to show a good code example. I don't find this very clear as-is. Commented May 8, 2017 at 15:40
  • Yes,it is possible to implement two interfaces in Java. But still it is not clear what you asking for, could you be a little bit more specific? May be by example? Commented May 8, 2017 at 15:40
  • I've rewritten your question to hopefully make it clearer. If I've misinterpreted your question, feel free to edit your question again :) Commented May 8, 2017 at 15:43
  • Thanks very much, that's right and great! Sorry about my English. Commented May 9, 2017 at 4:38

3 Answers 3

11

Two enforce a parameter to have 2 interfaces you have 2 basic options:

  1. Create a common interface that extends both and use that as your parameter type:

    interface FatherAndTeacher extends Father, Teacher { 
    }
    

    The problem with that is that objects that don't implement that interface don't match.

  2. Use generics where you can require matching objects to implement both interfaces:

    public  <T extends Father & Teacher> void foo(T person) {
      // Do something
    }
    

    Note that this only works with interfaces, i.e. you can't do T extends Number & String (2 classes). It works with one object boundary though, in which case the class must be first: T extends ConcretePerson & Father is ok but T extends Father & ConcretePerson is not (where ConcretePerson is a class).

Sign up to request clarification or add additional context in comments.

4 Comments

@Michael you're right, he didn't add it. I adapted the answer accordingly.
Thank you very very much! : ) ^v^, I get the success, and thank you very very much : ) ^v^.
@WaterMoon you're welcome. Note that you should accept one of the working answers then. :)
Sorry, first use, don't know about this.
2

You can use a composite interface by extending both of your Teacher and Father interfaces.

Here's an example:

interface Teacher
{
    void teach();
}

interface Father
{
    void makeBadJoke();
}

// ----- Composite interface! Doesn't add any methods.
interface TeacherAndFather extends Teacher, Father
{
    /*This can be empty*/
}

class Bob implements TeacherAndFather
{
    public void teach() { System.out.println("1 + 1 = 2"); }
    public void makeBadJoke() { System.out.println("Knock knock..."); } 
}

class Main
{
    private static void foo(TeacherAndFather foo)
    {
        foo.teach();
        foo.makeBadJoke();
    }

    public static void main (String... args)
    {
        foo(new Bob());
    }
}

2 Comments

That's a great way, thank you very much.I thought this way and sometimes I want create less interface.Thanks. : )
@WaterMoon You're welcome. Remember to click the check mark next to whichever answer solved your problem.
0

just use Object as your function parameter type:

void doSomething(Object param);

or using generic method:

<T> void doSomething(T param);

or let the two interface both inherit from one same interface.

public interface Person {}
public interface Teacher extends Person {}
public interface Father extends Person {}
public class Test {
    public void foo(Person p) {
        //
    }
}

3 Comments

Except in that case you can pass things to foo that are neither a Teacher or a Father.
yes, the foo function itself should check where parameter is instance of Teathcer or Father
Nice to be able to enforce it at compile-time though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.