0

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.

Tu.Ma.
  • 1,325
  • 10
  • 27
  • Duplicate of https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p – Törpetestű Nov 26 '17 at 17:17
  • 2
    Possible duplicate of [Is List a subclass of List? Why aren't Java's generics implicitly polymorphic?](https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p) – DontKnowMuchBut Getting Better Nov 26 '17 at 17:20
  • Thanks for pointing out that question, it was useful for learning purposes, but I explained in the EDIT what is missing from there. – Tu.Ma. Nov 26 '17 at 17:30

2 Answers2

1

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.

Progman
  • 16,827
  • 6
  • 33
  • 48
-2

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

diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • The interface is not accessible to me. – Tu.Ma. Nov 26 '17 at 17:20
  • why not if it is on the same package as interface A ? import your interface, the problem is that method is taken the class instead of the interface, or try to change the name , your class should not have the same name as your interface, try with BImpl, AImpl . – diegoveloper Nov 26 '17 at 17:22