Java methods awaiting multiple inheritance options

I do not know why I can not find the answer to this online.

I have classes that implement several methods, and I would like to write methods to expect them. I do not know how to do this, or if it is possible.

eg:

public void yellAtPet(<? extends Pet implements YellableAt> arg) {
    arg.yellAt("Don't go there!"); 
    arg.pet("Good Boy");
}
+3
source share
2 answers

This should work fine as a generic method without creating a generic class:

public <T extends Pet & YellableAt> void yellAtPet(T arg) {
    arg.yellAt("Don't go there!"); 
    arg.pet("Good Boy");
}
+5
source

Extensions are used for both interfaces and parent classes.

If you want to report multiple extensions, you need something like:

<T extends ClassA & InterfaceB>

To enforce this method, create a class:

public class MyClass<T extends something & somethingelse>{
    public void doSomething(T arg)
    {
         //call methods defined by either interface
    }
}
+7
source

All Articles