Why does Kotlin prohibit exposing restricted-visibility types?

The reason is this. Kotlin’s default visibility is public, and the goal is that you don’t have to pollute your internal and private classes with explicit visibility modifier. Declaring your class as private or internal should be enough to hide it from the outside world. Now, if this class happens to be open and you inherit from it a public class, then you accidentally leak details that were originally supposed to be private/internal to the outside world. This is error-prone (hard to notice) and that is what the above restriction is designed to fix.

It does cause some practical problems, so additional mechanisms are needed, like the one explained here: https://youtrack.jetbrains.com/issue/KT-22396

2 Likes