Search code examples
javac++type-systems

Merging interfaces, without merging


I was thinking, does C++ or Java have a way to do something like this

Interface IF1{
    ....
};

Interface IF2{
    ....
};


function f(Object o : Implements IF1, IF2){
    ...
}

meaning a typesystem that allows you to require implementation of interfaces.


Solution

  • You can do this in Java:

    public <I extends IF1 & IF2> void methodName(I i){
    
    ....
    
    }
    

    This way, you force I to implement your two interfaces, otherwise it won't even compile.