PDStat
PDStat

Reputation: 5825

Java interface methods passing super type

I have an interface that has methods like

public interface IFilterManager {
    public FilterAbs getFilter(String filterName);
    public void saveFilter(FilterAbs filter);
}

I then have a class that implements these.

public class FilterManager implements IFilterManager {
    @Override
    public JobFilter getFilter(String filterName) {
    //Do stuff
    }

    @Override
    public void saveFilter(JobFilter jobFilter)
    //Do stuff
    }
}

JobFilter is a concrete class of FilterAbs, eclipse has no problems with getFilter I'm confused why it has an issue with saveFilter. The error is

The method saveFilter(JobFilter) of type MappedJobFilterManager must override or implement a supertype method

Shouldn't it accept any type of FilterAbs as it's argument?

Upvotes: 0

Views: 902

Answers (3)

Masudul
Masudul

Reputation: 21961

To override you need exact method signature.

@Override
public void saveFilter(FilterAbs jobFilter)
//Do stuff
}

Upvotes: 0

Ross Drew
Ross Drew

Reputation: 8246

Because the method is different, it has a different argument and therefore different signature

public void saveFilter(FilterAbs filter);
public void saveFilter(JobFilter jobFilter)

Upvotes: 0

Adam Arold
Adam Arold

Reputation: 30528

The problem is that the signature is different.

So you have public void saveFilter(FilterAbs filter); in your interface and

public void saveFilter(JobFilter jobFilter) in your code.

Please note that public is also redundant in your interface. It is implicitly public by default.

You can use a generic interface to alleviate this problem:

public interface SomeInterface<T extends FilterAbs> {
    public T getFilter(String filterName);
    public void saveFilter(T filter);
}

Although it is not perfectly clear what are you trying to achieve.

Upvotes: 4

Related Questions