4

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?

8
  • 2
    Your interface itself doesn't make sense. Why are its methods taking parameters? Why of its own type?? Commented Sep 25, 2016 at 18:06
  • Please post your complete requirements because I think that you're missing first principles here. Commented Sep 25, 2016 at 18:06
  • Your getID and printID should take an IDedObject as the parameter. Commented Sep 25, 2016 at 18:07
  • @HovercraftFullOfEels I modeled the interface off the one from the Java docs. You can see that the Relatable interface contains a method which uses a Relatable object as a parameter. link Commented Sep 25, 2016 at 18:08
  • You can't change the type when you override; by definition, an override requires an identical signature with a super-type method. Commented Sep 25, 2016 at 18:08

4 Answers 4

2

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();
    }

}
Sign up to request clarification or add additional context in comments.

8 Comments

I had initially designed it this way, but then there is an error within my Article class "The type Article must implement the inherited abstract method IDedObject.getID()" , which to me implies that I can't pass a parameter into the function, right?
How does this answer the question?
@zcoon: you should only use a method that takes an IDedObject if you need to compare the current object with another IDedObject, for instance if you wanted to make the class Comparable.
@CKing: the question is an XY Problem and I'm trying to solve the underlying problem itself, and not solve the direct question which is barking up the wrong tree.
Thanks @HovercraftFullOfEels .. Embarrassingly, I had blanked on the fact that I could access Article's properties without passing in an instance of Article to the methods defined within itself.
|
2

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.

5 Comments

So a getter method takes a parameter? This does not make sense.
I am only answering the direct question being asked which is 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()
Probably the best solution would be to answer both, the direct question and the XY Problem issue.
Answering the direct question is mandatory. Any answer without that is incorrect.
Other way around. Yes we should answer both (and I've updated my answer to do so), but primarily we should help solve the xy issue first and foremost. Please see the various meta questions on this.
0

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.

Comments

-1

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);

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.