https://kotlinlang.org logo
#android
Title
# android
a

Aiden

11/26/2019, 10:23 AM
I have a ProductDao interface class which implemented(?) by ProductDatabase. Maybe I should not say implemented(?), because it is not using the
implements
word. 1. Why the ProductDatabase can use
abstract
to initialize
interface
class?
abstract fun productDao(): ProductDao
2. What does the line of code means and trying to do?
Copy code
@Database(entities = [Product::class], version = 1)
abstract class ProductDatabase : RoomDatabase() {
    abstract fun productDao(): ProductDao
}

@Dao
interface ProductDao {
    @Insert
    fun insert(product: Product)

    @Update
    fun update(product: Product)

    @Delete
    fun delete(product: Product)

    @Query("SELECT * FROM products")
    fun all(): LiveData<Array<Product>>

    @Query("SELECT * FROM products WHERE id = :id")
    fun get(id: Int): Product
}
b

bezrukov

11/26/2019, 10:41 AM
better to say ProductDatabase provides ProductDao Room generates
ProductDao_Impl
that
implements ProductDao
and
ProductDatabase_Impl
that
implements ProductDatabase
for you.
And answers: 1. because
ProductDatabase_Impl
implements this fun 2. In impl it simply creates
ProductDao_Impl(db)
You can simply review the generated code after assemble: navigate to mentioned classes by search OR simply click bottom arrow in the left bar near any abstract declaration
❤️ 1
a

Ayden

11/26/2019, 12:12 PM
Thank you!!
3 Views