'Why is it recommended to make the class that extends Room Database to abstract?
What is the reason behind it ? Is it for performance or compulsion? Please Explain
Solution 1:[1]
Because RoomDatabase
has defined methods, the inheriting class cannot be an interface. And because you write your undefined method in that abstract class, it cannot be a class.
public interface MyDatabase extends RoomDatabase // extend is not allowed for interface
public class MyDatabase extends RoomDatabase {
public abstract MyDao myDao(); // abstract method is not allowed for non abstract class
}
Solution 2:[2]
Because Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.
Room is an ORM, Object Relational Mapping library. In other words, Room will map our database objects to Java objects. Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.
Solution 3:[3]
First, we need to understand one thing about Abstract Classes in that they allow you to skip implementing some default methods of the parent class.
Unlike Interfaces
where you implement all the interface methods, in Abstract Classes world the child classes can choose to implement what is specific to the child class and leave out the other implementations if not needed.
If you do a deep dive on the parent RoomDatabase documentation you will note that it is the Base class for all Room databases and it has some methods not relavant to the child classes e.g. createOpenHelper()
, createInvalidationTracker()
, clearAllTables()
.
In fact the docs clearly mention that 'RoomDatabase provides direct access to the underlying database implementation but you should prefer using Dao classes'.
In simple terms, you should mark your RoomDatabase class as Abstract
to enable your class become flexible and skip implementing unnecessary methods of the RoomDatabase Base Class.
This is true because when you don't mark the class as Abstract, you will get an error and Android Studio will prompt you to implement the above Base Class methods.
So in Kotlin you will do something like this:
@Database(entities = [Note::class], version = 1)
abstract class NoteDatabase :RoomDatabase() {
//Property must be initialized or be abstrac
abstract val noteDAO:NoteDAO
}
Here the NoteDatabase
inherits from RoomDatabase to make it a RoomDatabase Object- it doesn't need parent's default implementations therefore it skips those because it is marked with Abstract.
NoteDatabase only needs to declare the an abstract noteDao
variable. This is so that NoteDatabase knows about DAO Interface and this variable is also used for testing.
Solution 4:[4]
When a class extends an abstract class, it needs to override all its abstract methods. But, in our case we don't need to implement abstract methods of RoomDatabase class. So, to overcome that we need to make our Room database class abstract.
abstract class ABC{
public abstract void methodA();
public abstract void methodB();
public abstract void methodC();
}
public class OurClass extends ABC{
public void methodA(){
System.out.println("Only methodA implementation");
}
}
This will give compile time error.
Solution 1: Implement all abstract methods of the Parent class in SubClass.
Solution 2: (Our case) Make SubClass abstract.
Solution 5:[5]
Consider using abstract classes if any of these statements apply to your situation:
- You want to share code among several closely related classes.
- You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
- You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Harsh Panchal |
Solution 3 | Tonnie |
Solution 4 | Divyansh Verma |
Solution 5 |