简体   繁体   中英

Error implementing Interface Methods in Class

I'm working on a simple project using Interfaces, but I am having an issue making my class conform to the interface.

My thought process is that since Article implements IDedObject, I should be able to pass an Article as a parameter in my overridden functions within my Article Class definition. Unfortunately this throws the error "The type Article must implement the inherited abstract method IDedObject.getID()"

Interface

public interface IDedObject{
    public int getID(IDedObject object);
    public void printID(IDedObject object);
}

Class

public class Article implements IDedObject{
private int articleID;
private String articleName;
private String authorName;

@Override
public int getID(Article article){
    return article.articleID;
}

@Override
public void printID(Article article){
    System.out.println(article.articleID);
}
}

What is missing or incorrect?

Only a guess since we don't have your requirements, but I think that your interface is broken, that your methods shouldn't require parameters much less parameters of its own type. Consider changing:

public interface IDedObject{
    public int getID(IDedObject object);
    public void printID(IDedObject object);
}

to:

public interface IDedObject{
    public int getID();
    public void printID();
}    

Then the implementation would be trivial

public class Article implements IDedObject{
    private int articleID;
    private String articleName;
    private String authorName;

    // constructor and other getter and setter methods here

    @Override
    public int getID(){
        return articleID;
    }

    @Override
    public void printID(){
        System.out.println("" + articleID);
    }
}

As for your compiler error -- the signature of any overridden methods must match those of the interface methods. So for instance in your Rectangle example in your link, if you extend that class or interface, then the method parameter must take the interface parameter as declared in the interface.

For example, say you had the following interface:

public interface FooInterface {
    int getValue();
    void printValue();
    int difference(FooInterface fi);
}

The concrete class that implements this interface must use a FooInterface parameter for the difference method. For example:

class FooClass implements FooInterface {

    private int value;

    @Override
    public int getValue() {
        return this.value;
    }

    @Override
    public void printValue() {
        System.out.println(String.valueOf(value));
    }

    @Override  // can't use FooClass for parameter here
    public int difference(FooInterface fi) {
        return value - fi.getValue();
    }

}

The getID and putID methods are not being overriden in Article class. They are being overloaded .

When you change the parameter type, it is an overload and not an override . This may seem a bit confusing at first but the key thing to understand is that Article inherits the two methods from the interface. When you change the parameter type, you are actually overloading these inherited methods rather than overriding them.

That said, the purpose of a getter method is to return the value of an instance variable and optionally perform some operations on this value before returning it.

Your override

public int getID(Article article)

Doesn't override the method of the interface because of a mismatch in the parameter - it should be IDedObject . You could use a generic parameter to IDedObject , and use a wildcard constraint to make sure it implements IDedObject , but as far as I know, there is no way to tell Java that you want to inherit with the same type as the wildcard.

You need to cast to the implementation of the interface (Article). The method signatures in the interface and the class need to be the same.

public interface IDedObject{
    public int getID(IDedObject object);
    public void printID(IDedObject object);
}

public class Article implements IDedObject{
    private int articleID;
    private String articleName;
    private String authorName;

    @Override
    public int getID(IDedObject object) {
        Article article = (Article) object;
        return article.articleID;
    }

    @Override
    public void printID(IDedObject object) {
        Article article = (Article) object;
        System.out.println(article.articleID);

    }
}

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