سؤال

In the below poll() method, my IDE is complaining about the fact that it returns JobSet. Tooltip reads: poll() in my.package.JobSetQueue clashes with poll() in java.util.concurrent.PriorityBlockingQueue; attempting to use an incompatible return type

How come I can't use whatever return type I want here?

public class JobSetQueue extends PriorityBlockingQueue<FIFOEntry<JobSet>> {
    public JobSetQueue() {
        super(1, new JobSetComparator());
    }

    public boolean add(JobSet jobSet) {
        return super.add(new FIFOEntry<JobSet>(jobSet));
    }

    public JobSet poll() {
        /*FIFOEntry<JobSet> entry = super.poll();
        return entry.getEntry();*/
        return super.poll().getEntry();
    }
}



public class FIFOEntry<T> {
    final static AtomicLong seq = new AtomicLong();
    private final long sequenceNumber;
    private final T entry;

    public FIFOEntry(T entry) {
        sequenceNumber = seq.getAndIncrement();
        this.entry = entry;
    }

    public long getSequenceNumber() {
        return sequenceNumber;
    }

    public T getEntry() {
        return entry;
    }
}
هل كانت مفيدة؟

المحلول

PriorityBlockingQueue already has a method poll() with a generic return type. You cannot override a method and change its return type to some incompatible type, see the JLS section for method overrides and the section for method return types return-type-substitutable.

In your case, PriorityBlockingQueue<FIFOEntry<JobSet>> would expect poll() to return FIFOEntry<JobSet>.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top