Kotlin: How to deal with 'protected' property exposes its internal return type

1.6k Views Asked by At

Having a base class I would like it and its descendent class be visible only internally:

internal abstract class BaseClass
internal open class Class_A: BaseClass()
internal open class Class_B: Class_A()

In place where the list of Class_A (may also contains Class_B in it), would like to make it protected for its own descendent class to access this list

open class User {
    // got error: 'protected' property exposes its internal return type" 
    protected var class_A_list: List<Class_A>? = null
}

class User_descendent: User() {
    // can access the class_A_list
}

How to let the descendent class access the instance of some "internal" class?

1

There are 1 best solutions below

7
stinepike On

The above error is protecting the internal classes to be accessed by other classes which are not in the same module of the internal class. If it was allowed, then you can't guarantee that the class User would be only inherited by the classes in the same module.

So if you want to make the class_A_list protected, you have to make the User class internal. By doing so, it will guarantee that, User will be inherited by the classes which is in the same module. The following should be fine:

internal open class User {
   protected var class_A_list: List<Class_A>? = null
}