Visibility Modifier

Visibility Modifier

In Kotlin, visibility modifiers are used to control the visibility of a class, its members (properties, functions, and nested classes), and its constructors. The following are the visibility modifiers available in Kotlin:
  1. private: The private modifier restricts the visibility of a member to the containing class only. A private member cannot be accessed from outside the class.
  1. internal: The internal modifier restricts the visibility of a member to the same module. A module is a set of Kotlin files compiled together.
  1. protected: The protected modifier restricts the visibility of a member to the containing class and its subclasses.
  1. public: The public modifier makes a member visible to any code. This is the default visibility for members in Kotlin.
In Kotlin, visibility modifiers are used to restrict the accessibility of classes, objects, interfaces, constructors, functions, properties, and their setters to a certain level. No need to set the visibility of getters because they have the same visibility as the property.
 
  • If there is no specified modifier then by default it is public. Let’s start discussing the above modifiers one by one.