Kotlin Sealed Class in Inner Class

Jul 24, 2018
This type is sealed, so it can be inherited by only its own nested classes or objects

When Sealed Classes is declared in inner class, the following error is shown

Cannot access '': it is 'private' in 'ViewItem'

This type is sealed, so it can be inherited by only its own nested classes or objects

sealed class ViewItem(val resource: Int)class Image(val imageResource: String): ViewItem(R.layout.content_pin_list_item_image)class Property(val icon: Int?, val value: String): ViewItem(R.layout.content_pin_list_item)

The following declaration must be used for inner class.

class PinActivity : AppCompatActivity() {    sealed class ViewItem(val resource: Int) {        class Image(val imageResource: String): ViewItem(R.layout.content_pin_list_item_image)        class Property(val icon: Int?, val value: String): ViewItem(R.layout.content_pin_list_item)    }}

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.