Kotlin for Android Developers: Visibility Modifiers

Last updated 224 days ago by Pablo Costa

kotlin

What is a visibility modifier?

A visibility modifier (a.k.a access modifiers or access specifiers) is a concept not tied to any specific programming language. It is a language keyword that precedes a declaration and defines the scope from where it is visible -thus can be accessed. Visibility modifiers facilitate encapsulation.

There are four visibility modifiers in Kotlin: private, protected, internal and public. The default visibility modifier is public.

Local variables, functions, and classes cannot have visibility modifiers in Kotlin.

Private

The declarations marked with the private modifier are visible inside the file/class containing the declaration.

Protected

The declarations marked with the protected modifier are visible inside the file containing the declaration and subclasses. This modifier is not allowed for top-level declarations.

Internal

The declarations marked with the internal modifier are visible everywhere in the same module.

Read full Article