Search code examples
androiddatabase-designrealm

Is it okay to extend from classes that extend RealmObject in Java?


Let me explain with the help of a example. I have a Transaction Class (Realm table) which extends RealmObject. I want to create a DeletedTransaction Class (Realm table) which will store transactions after they are deleted. Instead of making two seperate Classes (Transaction and DeletedTransaction) which both extend RealmObject, can I just make the Transaction Class extend RealmObject and then can I make the DeletedTransaction Class extend the Transaction Class?

I know this is possible from the Java perspective, but I wanted to know from the Realm perspective. What are the pros/cons of such an approach?


Solution

  • Inheritance in RealmObject is not supported. Issue#761

    Instead of extending RealmObject, your classes can implement the RealmModel interface, adding the @RealmClass annotation:

    @RealmClass
    public class User implements RealmModel {
    
    }
    

    Reference-Docs