Reputation: 1
I have a class Message.java
:
public class Message extends RealmObject implements Serializable {
@PrimaryKey
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("user_id")
@Expose
private User user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
However, when saving Message
objects in Realm, I want to save them under a different class name, NotificationMessage
(which should have all the same properties and methods as the Message
class), like:
public class NotificationMessage extends Message {
}
But this doesn't seem to work. How can I create the NotificationMessage
class without copying and pasting all of the properties and methods of the Message
class?
Upvotes: 0
Views: 32
Reputation: 961
It seems that you hit this issue https://github.com/realm/realm-java/issues/761
Currently, Realm doesn't support that kind of inheritance, but what some people in that thread did was to use an Interface which holds all the methods that are common to your classes.
In your particular case, you have to convert NotificationMessage
into an interface to hold the methods of Message and then make Message
implements the NotificationMessage
.
public interface NotificationMessage {
public Integer getId() ;
public void setId(Integer id) ;
public User getUser() ;
public void setUser(User user) ;
}
public class Message extends RealmObject implements Serializable, NotificationMessage {
@PrimaryKey
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("user_id")
@Expose
private User user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Upvotes: 1