Is Java an ambiguous type for a method?

EDIT: This is not a problem at all with the code, but with an error in the Groovy Eclipse plugin ( http://jira.codehaus.org/browse/GRECLIPSE-373 )

Eclipse gives me a weird error message about ambiguous types in a Java program, and I really don't understand why. I have an interface that takes a generic parameter indicating what type of data it returns.

public interface InterfaceA<T> {
    T getData();
}

One of the implementations looks like this:

public class Impl<T extends AnotherClass> implements InterfaceA<Collection<T>> {
    public Collection<T> getData() {
       // get the data
    }
}

There is also a container for the interface

public class Container<T extends InterfaceA>
{
    private T a;

    public Container(T a) {
        this.a = a;
    }

    public T getA() {
        return a;
    }
}

This leads to the error "getData is ambiguous."

Container<Impl<AnotherClass>> c = new Container(new Impl<AnotherClass>());
Collection<AnotherClass> coll = c.getA().getData();

I'm at a standstill on this.

+3
source share
6 answers

Collection<T> getData(), Impl, . , .

+2

(JDK 1.5), , . :

public interface InterfaceA<T> {
    T getData();
}

public static class Impl<T extends Date> implements InterfaceA<Collection<T>> {
    public Collection<T> getData() {
        return null;
    }
}

public static class Container<T extends InterfaceA> {
    private T a;

    public Container(T a) {
        this.a = a;
    }

    public T getA() {
        return a;
    }

}

public static void main(String[] args) {
    Container<Impl<Date>> c = new Container<Impl<Date>>(new Impl<Date>());
    Collection<Date> coll = c.getA().getData();
}
0

, , . , Eclipse , .

Windows > > Java > p > /. " " , Eclipse ( ). "". Eclipse.

: , , ( ) , : " . :". :

Container<Impl<Date>> c = new Container<Impl<Date>>(new Impl<Date>());

( java.util.Date "AnotherClass" ).

0

[, ]

, :

public class Impl<T extends AnotherClass> implements InterfaceA<Collection<T>> {
    Collection<T> getData() {
       // get the data
    }
}

(Eclispe 3.4, OS X, 1.5), , :

temp.tests;

import java.util.Collection;

public interface InterfaceA <T> {

    T getData();

    public static final class AnotherClass {}

    public static final class Impl<T extends AnotherClass> 
             implements InterfaceA<Collection<T>>
   {
        public Collection<T> getData () {
            return null;
        }
    }

    public static class Container<T extends InterfaceA>
    {
        private T a;
        public Container(T a) { this.a = a; }
        public T getA() { return a; }
    }

    public static final class Test {
        public static void main (String[] args) {
            Container<Impl<AnotherClass>> c = new Container(new Impl<AnotherClass>());
            Collection<AnotherClass> coll = c.getA().getData();
        }
    }
}
0

, Eclipse 3.5.0, JDK 1.6.0.14 ( getData()).

(Project/Clean in Eclipse). , Eclipse Java.

-

0

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


All Articles