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.
-
I think you'll need to show a good code example. I don't find this very clear as-is.Carcigenicate– Carcigenicate2017年05月08日 15:40:29 +00:00Commented 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?Vasily Vlasov– Vasily Vlasov2017年05月08日 15:40:30 +00:00Commented 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 :)Michael– Michael2017年05月08日 15:43:20 +00:00Commented May 8, 2017 at 15:43
-
Thanks very much, that's right and great! Sorry about my English.WaterMoon– WaterMoon2017年05月09日 04:38:10 +00:00Commented May 9, 2017 at 4:38
3 Answers 3
Two enforce a parameter to have 2 interfaces you have 2 basic options:
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.
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 & Fatheris ok butT extends Father & ConcretePersonis not (whereConcretePersonis a class).
4 Comments
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());
}
}
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) {
//
}
}