简体   繁体   中英

Returning objects implementing interfaces

I have a given set of interfaces, and I have to implement them. The interfaces are the following:

package com.something.interfaces;
public interface A {
    void foo();
    Set<B> bar();
}

package com.something.interfaces;
public interface B {
    void foobar();
}

And my implementations are the following:

package com.something.implementations;
public class A implements com.something.interfaces.A {
    @Override 
    public void foo(){
        //Something
    }

    @Override
    public Set<com.something.interfaces.B> bar(){
        Set<com.something.implementations.B> something = new HashSet<com.something.implementations.B>();
        //...
        return something;
    }
}

package com.something.implementations;
public class B implements com.something.interfaces.B {
    @override
    public void foobar(){
        //Something
    }
}

The problem is in the method bar() of the implemented class. It gives me the error:

Type mismatch: cannot convert from java.util.Set<com.something.interfaces.B> to java.util.Set<com.something.implementations.B>

I am 100% sure that the signature are respected, I tried just leaving the auto generated stubs to check. Maybe there is something I am missing on the theory, but how can I implement the method if this does not work?

Thanks

EDIT As this question has been marked as a possible duplicate, I will explain the difference with it.

That was a generic question, I have a really specific problem here. The interfaces are not accessible by me, because I am writing code that will be submitted and autograded using original interfaces, I won't submit any modifications.

Knowing what that question pointed out does not help me understand how to escape from this situation, because given these interfaces, I need to return my implemented versions of them, and that question really gave me no clue on how to do that.

EDIT2

In the comments, it has been pointed out that my requirements are not clear. I'll try to clarify.

I can't change the interfaces, I need to be sure that this is a problem of interfaces and that there is not a workaround for this. If this is the case, I will try to reason with my professor.

Change your implementation of the bar() method to this:

@Override
public Set<com.something.interfaces.B> bar(){
    Set<com.something.interfaces.B> something = new HashSet<com.something.interfaces.B>();
    //...
    return something;
}

You can fill the Set in something with any object which implements the com.something.interfaces.B interface, including your implementation class com.something.implementations.B .

Change your interface A like this:

public interface A {
    void foo();
    Set<com.something.interfaces.B> bar();
}

Change the names of your interfaces or your implementations to avoid confusion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM