Home > Back-end >  Is there a way to not inherit a specific property?
Is there a way to not inherit a specific property?

Time:12-16

enter image description here

I'm trying to group my classes like images.

It is a structure in which one header and several items are gathered in one group to form a list.

If the Header property and Item list properties are placed in the sealed class, the subclass inherits this, so the structure becomes strange.

So, I want to make it impossible to inherit only Header property and Item list property in sealed class. Is there a way?

GroupedItem

sealed class GroupedItem(val layoutId : Int = 1) {
    val header: Header = Header() // I want to make inheritance impossible
    val itemList: List<Item> = listOf() // I want to make inheritance impossible

    data class Header(
        val id: String = "",
    ) : GroupedItem(someValue)

    data class Item(
        val id: String = "",
        val set: Int = 1,
    ) : GroupedItem(someValue)
}

Grouping example

for (i in 0 until headerNumber) {
    val headerText = "Header $i"
    val header = GroupedItem.Header(headerText)
    val itemList = arrayListOf<GroupedItem.Item>()
    val indexNumber = Random.nextInt(2, 5)
    for (j in 0 until indexNumber) {
        val itemText = "Item $j"
        itemList.add(GroupedItem.Item(itemText))
    }
    val groupedItem = GroupedItem(header, itemList)
    groupedItemList.add(groupedItem)
}

ADDED

Main

fun main() {
    val test = RoutineItem.Header()

    println(test.id)
}

ERROR

Exception in thread "main" java.lang.StackOverflowError
    at RoutineItem$Header.<init>(hello.kt)
    at RoutineItem$Header.<init>(hello.kt:18)
    at RoutineItem.<init>(hello.kt:15)
    at RoutineItem.<init>(hello.kt)
    at RoutineItem$Header.<init>(hello.kt:22)
    at RoutineItem$Header.<init>(hello.kt:18)
    at RoutineItem.<init>(hello.kt:15)
    at RoutineItem.<init>(hello.kt)
``

CodePudding user response:

The way you're defining it, a Header is a GroupedItem, and every GroupedItem has a Header, which is incorrect, right? It seems like you want one of these:

GroupedItem represents a category of items that go in your groups, i.e. Headers and ItemLists:

You can use an interface, but a sealed class is fine for this (and gives you the GroupedItem.Thing namespacing). Since GroupedItem is a general category, it shouldn't contain a Header or anything else, just like a Vehicle shouldn't have a Car property


GroupedItem represents a structure that contains a Header and an ItemList (possibly optional):

In this case, Header isn't a GroupedItem at all (same as a Wheel isn't a Car) and shouldn't inherit from it - it has no relation to that class. GroupedItem just happens to include a Header as a property.

If you want the GroupedItem.Thing naming scheme to keep things organised, you can just make Header a class within GroupedItem (not an inner class though):

class GroupedItem(
    val header: Header = Header(),
    val itemList: List<Item> = listOf()
){

    data class Header(
        val id: String = "",
    )

    data class Item(
        val id: String = "",
        val set: Int = 1,
    )
}

you'll have to sort out where layoutId is coming from though.


honestly though, I think you probably want both of these things:

  • GroupedItem to represent things that go in groups
  • Group for an actual concrete group of those things
class Group(
    val header: Header = Header()
    val itemList: List<Item> = listOf()
)

sealed class GroupedItem(val layoutId : Int = 1) {
    data class Header(
        val id: String = "",
    ) : GroupedItem(someValue)

    data class Item(
        val id: String = "",
        val set: Int = 1,
    ) : GroupedItem(someValue)
}
  • Related