Classes, objects, interfaces, constructors, and functions, as well as properties and their setters, can have visibility modifiers.
💡 Getters always have the same visibility as their properties.
There are four visibility modifiers in Kotlin: private, protected, internal, and public. The default visibility is public.
Packages
- If you don’t use a visibility modifier,
publicis used by default, which means that your declarations will be visible everywhere. - If you mark a declaration as
private, it will only be visible inside the file that contains the declaration. - If you mark it as
internal, it will be visible everywhere in the same module. - The
protectedmodifier is not available for top-level declarations.
// file name: example.kt
package foo
private fun foo() { ... } // visible inside example.kt
public var bar: Int = 5 // property is visible everywhere
private set // setter is visible only in example.kt
internal val baz = 6 // visible inside the same moduleClass members
privatemeans that the member is visible inside this class only (including all its members).protectedmeans that the member has the same visibility as one marked asprivate, but that it is also visible in subclasses.internalmeans that any client inside this module who sees the declaring class sees itsinternalmembers.publicmeans that any client who sees the declaring class sees itspublicmembers.
