6

In my app, I am using realm as a database platform. Realm has been great thus far- super easy to use, make queries, etc.-though, I have two qualms with it that I am trying to work around.

1) Realm does not support inheritance in model object classes which extend RealmObject. - I am wondering if any developers out there have used composition as a workaround to the inheritance issue and if its worked for them. - In other words, say I have a class "car" which extends RealmObject and then I have other classes such as "honda", "toyota", etc. Does it make sense to make separate car objects for each of these which contain either a honda, toyota, etc.?

2) Realm only supports getters and setters in model object classes - My current workaround for this is by creating static methods within the model object classes. - In other words, say I want to modify a honda's color, I would call something such as, Honda.updateColor(honda, blue). - ^ Is this sloppy, or is this essentially the only way to handle such methods.

Really any feedback would be great!

  • 1
    1) The related Realm issue is https://github.com/realm/realm-java/issues/761 . I don't think inheritance can totally be replaced by composition, however it could be helpful in same cases. The `RealmObject` and `RealmList` field in a model is also typed, you cannot do `ExtendCar extendedCar; setExtendCar(NewTypeHondaCar newCar)` 2) The feature "better object" has already been merged to Realm's master, it will be released very soon in 0.88.0. You can try with snapshot before release. With this feature, you don't have to write any setters/getters for `RealmObject` . – beeender Mar 07 '16 at 02:55
  • For 1) I understand that you could not do " ... extend car", though you could create a car object which extends realm object and a honda object which also extends realm object. You could then create a car object which contains a honda object, and set both objects to have the same unique identifier--or @primarykey. Does this sound like a viable solution? Also for 2) that's awesome! i have been waiting for this for quite some time now. –  Mar 07 '16 at 15:08
  • For 1) I think that would work. `Honda` could have `Car` for common fields, and with the 0.88.0, `Honda` can implements some interface like `getCarProps` which could also be implemented by `Toyota`. Almost like inheritance then ;) – beeender Mar 08 '16 at 02:02

1 Answers1

4

A workaround I've used (in a nutshell)

Composition + Interface inheritance would allow you to get some polymorphism benefits back to your realmObjects.

Some code demonstration

interface IPerson {

    String getName();
}

class Person extends RealmObject implements IPerson {

    String name;

    @Override
    public String getName() {
        return name;
    }
}

interface IWorker extends IPerson {
    int getSalary();
}

class Worker extends RealmObject implements IWorker {

    Person person;
    int salary;

    @Override
    public String getName() {
        return person.getName();
    }

    @Override
    public int getSalary() {
        return salary;
    }
}

Note

PrimaryKeys unfortunately have to be duplicated.

Check this answer of mine to get more details about this workaround.