This might be a dumb question, but I’m having a ha...
# getting-started
b
This might be a dumb question, but I’m having a hard time understanding visibility rules in Kotlin. 1. You cannot use a less visible type as a parameter of a function or class. This makes total sense because my function will be unusable if I do so. Essentially, we can access the function but not its parameters. 2. What confuses me, though, is that we cannot even use a base class with less visibility, even if we don’t expose anything from it. For example, this is allowed:
Copy code
internal class ABC {
  fun doSomething() = println("Doing something")
}

public class B() {
  private val a = ABC()
}
But this isn’t:
Copy code
internal class ABC

class ZYX : ABC
j
The super class itself is exposed to consumers, so it needs adequate visibility
👆 1
b
Could you give some examples that might create an issue if this is allowed?
d
Let’s turn the questing the other way round. What would you need to inherit that isn’t exposed? If clients of your class don’t need to know about its super type, then probably that should be a member rather than a parent. Composition over inheritance. If the clients of the class DO need to know, or at least need to know some of the methods on it, the. Maybe a public interface with delegation would be the way.
💯 1
e
Copy code
public interface Foo
private open class ABC : Foo
public class ZYX : ABC()
then externally
Copy code
val foo: Foo = XYZ()
should this be legal or not? it can't be determined without the caller seeing
ABC