Sobes.tech
Junior

What access modifiers are available for types in Kotlin?

sobes.tech AI

Answer from AI

  • public: Visible everywhere. Default.
  • private: Visible only within the current file or within a class/interface (for members).
  • protected: Visible within the class/interface, its subclasses, and module-commands (companion objects). Not applicable to top-level declarations.
  • internal: Visible within a single module.
// Top-level declaration (applies to the file)
public class PublicClass // Visible everywhere (implicit public)
internal class InternalClass // Visible within the module
private fun privateFunction() {} // Visible only in this file

class MyClass {
    public val publicProperty: Int = 1 // Visible everywhere
    internal val internalProperty: Int = 2 // Visible within the module
    private val privateProperty: Int = 3 // Visible only in MyClass
    protected val protectedProperty: Int = 4 // Visible in MyClass and its subclasses

    fun publicMethod() {} // Visible everywhere (implicit public)
    internal fun internalMethod() {} // Visible within the module
    private fun privateMethod() {} // Visible only in MyClass
    protected fun protectedMethod() {} // Visible in MyClass and its subclasses
}
What access modifiers are available for types in… - sobes.tech