Common, which is required to implement two interfaces

I have several interfaces, for example

interface CanFly{ .. }
interface CanRun{ .. }

and many implementations (made by others)

class Dog implements CanFly{ .. }
class Duck implements CanFly, CanRun{ .. }

Now at some point I need to use generic, which requires types that implement two interfaces.

 class FlyAndRunHandler <ANIMAL extends CanFly AND CanRun>{
    void performAction(ANIMAL animal){
       animal.fly();
       animal.run();
    }
 }

In Scala, it will look like ANIMAL extends CanFly with CanRun. Is there a way to archive this in Java. If necessary, I could write additional interfaces, but there is no way to add them to animal classes.

+4
source share
1 answer

you can use Multiple Boundsas<T extends CanFly & CanRun>

: , CanFly & CanRun, , <T extends className & CanFly & CanRun>,

+3

Source: https://habr.com/ru/post/1657383/


All Articles