简体   繁体   中英

why am i not able to implement a class in a class ,which already extends a class

This is my DBInfo class:

public class DBInfo  stars table
 static final String STARS = "stars";
 static final String FIRST_NAME = "first_name";
 static final String LAST_NAME = "last_name";

 static final String CREATE_STAR_TABLE = "CREATE TABLE " + STARS + 
                                                    "(" + _ID + " integer primary key autoincrement, " 
                                                        + FIRST_NAME + " text not null, "
                                                        + LAST_NAME + " text not null);";

 //stars in movies
 static final String STARS_IN_MOVIES = "stars_in_movies";
 static final String STAR_ID = "star_id";
 static final String MOVIE_ID = "movie_id";
 static final String CREATE_STAR_IN_MOVIE_TABLE = "CREATE TABLE " + STARS_IN_MOVIES + 
                                                        "(" + STAR_ID + " integer not null, "
                                                            + MOVIE_ID + " integer not null, " 
                                                            + "FOREIGN KEY (" + MOVIE_ID + ") REFERENCES "+ MOVIES +" (" + _ID + ") "
                                                            + "FOREIGN KEY (" + STAR_ID + ") REFERENCES "+ STARS +" (" + _ID + "));";

 //statistics table
 static final String STATISTICS = "statistics";
 static final String RIGHT = "right";
 static final String WRONG = "wrong";
 static final String CREATE_STATISTICS_TABLE = "CREATE TABLE " + STATISTICS + 
                                                    "(" + _ID + " integer primary key autoincrement, " 
                                                    + RIGHT + " int not null default 0, "
                                                    + WRONG + " int not null default 0);";

}

And now I want to implement this class toy DBAdapter class which already extends SQLiteOpenHelper

public class DbAdapter extends SQLiteOpenHelper implements DBInfo{

    private static final String DATABASE_NAME = "quizdb";
    private static final int DATABASE_VERSION = 9;

    private static final String MOVIES_FILE = "movies.csv";
    private static final String STARS_FILE = "stars.csv";
    private static final String STARS_IN_MOVIES_FILE = "stars_in_movies.csv";

    private static SQLiteDatabase mDb;
    private static Context mContext;

    public static SQLiteDatabase getSQLiteDatabase(Context ctx){
        if(mDb == null){
            new DbAdapter(ctx);
        }
        return mDb;
    }

And I am getting the following error:

The type DBInfo cannot be a superinterface of DbAdapter; a superinterface must be an interface

You cannot implement class .

Make DBInfo an interface

implements DBInfo

This won't work since you can implement only interface and your DBInfo is class . So you have only choice:

  • Change your DBInfo class to interface

In your case only way is to change it to interface 1 or change whole application logic . By the way it's very good practise to create interface that will hold static constants:

public interface DBInfo {

   public static final String STARS = "stars";
   public static final String FIRST_NAME = "first_name";
   public static final String LAST_NAME = "last_name";
}

1 Because Java doesn't support multi-inheritance as was suggested.

You can only implement interfaces, not classes. This would seem like a situation where you should try composition instead of inheritance (have an instance of DBInfo in DbAdapter instead of inheriting it).

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