abstract method used without implementation

[亡魂溺海] 提交于 2021-01-28 08:36:41

问题


I was learning the Room Database for Android app development through a tutorial and came across an abstract method being used without its implementation. Following are the files:

  1. AppDatabase.java

    @Database(entities = {Contact.class}, version = 1)
    public abstract class AppDatabase extends RoomDatabase {
        private static final String DATABASE_NAME = "database.db";
        private static AppDatabase database;
    
        **public abstract ContactDao getContactDao();**
    
        public static AppDatabase getInstance(Context context) {
        if(database == null)
        {
            database = Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
                .allowMainThreadQueries().build();
        }
        return database;
      }
    }
    

The method getContactDao is being used in another file:

  1. ContactsFragment

    public class ContactsFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_contacts, container, false);
    
    AppDatabase database = AppDatabase.getInstance(getContext());
    **ContactDao contactoDao = database.getContactDao();**
    return view;
    }
    }
    

Here is the ContactDao class:

    public interface ContactDao {

        @Query("SELECT * FROM contact")
        List<Contact> getAll();

        @Insert(onConflict = OnConflictStrategy.REPLACE)
        void insert(Contact contact);

        @Update
        void update(Contact contact);

        @Delete
        void delete(Contact contact);

    }

My question is what does the getContactDao method do? And shouldn't the abstract class be implemented before being used?

来源:https://stackoverflow.com/questions/48790457/abstract-method-used-without-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!