Search code examples
javaandroidgreendaoimplements

Error when implementing interfaces with greenDAO


I'm trying to use greenDAO's implementsInterface method, here is most of my main generator class:

private static void addTables(final Schema schema) {
    Entity photo_pronoun = addCard(schema);
    Entity simple_pronoun = addSimpleCard(schema);
    Entity original_pronoun = addOriginalCard(schema);

    //implementsInterface method
    original_pronoun.implementsInterface("addNewCard");
    simple_pronoun.implementsInterface("addNewCard");
}

private static Entity addCard(final Schema schema) {
    Entity card = schema.addEntity("addNewCard");
    card.addIdProperty().primaryKey().autoincrement();
    card.addStringProperty("cardName").notNull();
    card.addStringProperty("cardSpeech");
    card.addByteArrayProperty("cardIcon");

    return card;
}
private static Entity addSimpleCard(final Schema schema) {
    Entity card = schema.addEntity("addSimpleCard");
    card.addIdProperty().primaryKey().autoincrement();
    card.addStringProperty("cardName").notNull();
    card.addStringProperty("cardSpeech");
    card.addByteArrayProperty("cardIcon");
    return card;
}

private static Entity addOriginalCard(final Schema schema) {
    Entity card = schema.addEntity("addOriginalCard");
    card.addIdProperty().primaryKey().autoincrement();
    card.addStringProperty("cardName").notNull();
    card.addStringProperty("cardSpeech");
    card.addByteArrayProperty("cardIcon");
    return card;
}

When I run this to create my files I get an error in original_pronoun and in simple_pronoun on my first line at addNewCard:

interface expected here public class addOriginalCard implements addNewCard {

I get that error because its not an interface, but I'm confused on how to fix it. The implementsInterface method says it takes a string but I've tried this and the database name with no joy. Can anyone tell me what I need to do here?


Solution

  • This is not a greenDAO problem: addNewCard is a class, not an interface. If your model class needs to inherit from another class, you have to use setSuperclass() method. Example:

    original_pronoun.setSuperclass("addNewCard");
    

    Note that greenDAO doesn't support another entity as a super class yet, if this is your intention.

    Check greenDAO docs for Inheritance and Interfaces.
    See also this question: Implements vs. Extends. When to use? What's the Difference?