2
IBaseA   <--- Interface
CBaseB   <--- Concrete base class

ChildA implements IBaseA{
    //fields and getters, setters
}

ChildB extends CBaseB, implements IBaseA{
    //fields and getters, setters  
}

TestClass implements RealmModel{
    private IBaseA child_obj;
}

The intention for making TestClass this way was to be able to assign any of ChildA or ChildB objects to TestClass.child_obj, and still be able to let ChildA and ChildB implement other interfaces as required.

However, this causes a compile-time exception

Error:(12, 8) error: Type 'in.avanti_app.student_companion.realmClasses.TestClass' of field 'child_obj' is not supported

How we can achieve the above intention?

0

2 Answers 2

2

Polymorphism and inheritance are not supported by Realm. You can follow this issue for updates: https://github.com/realm/realm-java/issues/761

Generally we recommend Composition instead: https://en.wikipedia.org/wiki/Composition_over_inheritance, but in your situation that probably isn't ideal since it would look something like this:

public class IBaseA extends RealmObject { ChildA childA; ChildB childB; }

0
0

Create a RealmObject for each concrete class, flattening the bases into the RealmObjects. Share the field accessors with common interfaces.

Read this github comment

and

Composition over inheritance for RealmObjects with Gson serialization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.