```sealed class NotificationData(val contactDispla...
# codereview
j
Copy code
sealed class NotificationData(val contactDisplayName: String) {
    class Message(contactDisplayName: String, val contentMessage: String) : NotificationData(contactDisplayName)
    class Join(contactDisplayName: String) : NotificationData(contactDisplayName)
    class File(contactDisplayName: String) : NotificationData(contactDisplayName)
    class Audio(contactDisplayName: String) : NotificationData(contactDisplayName)
}
Is there a better way to initialize the
contactDisplayName
property ?
contactDisplayName
is defined in the base class and each child class "override" it
m
The child classes in your example don’t override the property, they only have ctor parameters passed to the super ctor. If you want to cut down some boilerplate you could eliminate the property from the super class and define it in every subclass where needed:
Copy code
sealed class NotificationData {
    class Message(val contactDisplayName: String, val contentMessage: String) : NotificationData()
    class Join(val contactDisplayName: String) : NotificationData()
    class File(val contactDisplayName: String) : NotificationData()
    class Audio(val contactDisplayName: String) : NotificationData()
}
this way you can have a subclass without that property, if you want to. Otherwise there’s no good alternative to your original code, I suppose.
j
Thanks for the answer. I tried this before asking, but in my usage code of the class, I need to get
contactDisplayName
from base class, to avoid boilderplate code 😄
👍 1