Home » Android » android – Realm model classes must either extend RealmObject or implement RealmModel to be considered a valid model class

android – Realm model classes must either extend RealmObject or implement RealmModel to be considered a valid model class

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am getting below compilation error

Realm model classes must either extend RealmObject or implement
RealmModel to be considered a valid model class

In this particular scenario

class Animals extends RealmObject { 

}

class Dog extends Animals { 

}

I am getting this error for the Dog class.

That error appears when I am indirectly extending RealmObject. i.e Dog is extending Animals (which in turns extends RealmObject), not RealmObject directly.

How to&Answers:

Inheritance / Polymorphism is still an open issue in Realm,
Check this link https://github.com/realm/realm-java/issues/761

Answer:

While the issue has not been closed yet, it is recommended to use composition instead of inheritance: F.ex. you could have

public interface Animalistic {
    makeNoise();
}

which is then implemented by each animal.