简体   繁体   中英

Is it possible to extend some other class and implement RealmModel?

I'm trying to extend my model from a BaseModel and then implement RealmModel .

@RealmClass
public class User extend BaseModel implements RealmModel

But it is throwing an error:

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

Please help.

As mentioned in Realm's GitHub repository, inheritance and polymorphism are not implemented in Realm just yet.

As one of the contributors described it :

Realm currently is using byte code manipulation to replace field accessors and using annotation processor to create proxy classes. Enabling RealmObject to extend from the other class/abstract class will increase a bit complexity of Realm. So it is not implemented right now. But I agree it is something we should support.

You can read more here .

I guess that for the time being, you will have to think of another way to write your classes.

It is very unfortunate, but RealmModel exists so that you don't need to inherit properties and methods from RealmObject .

I tried extending a base class that only contained fields annotated with @Ignore , but even that is not supported at the moment .

Only way to "share fields" is to share the accessors through interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM