![]() |
VOOZH | about |
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:
Note: If no visibility modifier is specified, Kotlin uses public by default.
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.
There are four visibility modifiers in Kotlin.
Let's start discussing the above modifiers one by one.
In Kotlin, the default modifier is public. It is possibly the most frequently used modifier in the entire language and there are additional restrictions on who can see the element being modified. Unlike Java, in Kotlin there is no need to declare anything as public β it is the default modifier, if we donβt declare another modifier - public works the same in Kotlin, as in Java. When we apply the public modifier to top-level elements - classes, functions or variables declared directly inside a package, then any other code can access it. If we apply the public modifier to a nested element - an inner class, or function inside a class - then any code that can access the container can also access this element.
Access Scope:
Example:
Here, Class A is accessible from anywhere in the entire code, the variable int1, and the function display() are accessible from anything that can access classes A.
In Kotlin, private modifiers allow only the code declared insidethe same scope, access. It does not allow access to the modifier variable or function outside the scope. Unlike Java, Kotlin allows multiple top-level declarations in the same file - a private top-level element can be accessed by everything else in the same file.
Access Scope:
Example:
Output:
Cannot access 'int': it is private in 'A'Here, Class A is only accessible from within the same source file, and the int variable is only accessible from the inside of class A. When we tried to access int from outside the class, it gives a compile-time error.
In Kotlin, the internal modifier is a newly added modifier that is not supported by Java. Marked as internal means that it will be available in the same module, if we try to access the declaration from another module it will give an error. A module means a group of files that are compiled together.
Access Scope:
Note: Internal modifier benefits in writing APIs and implementations.
Example:
Here, Class A is only accessible from inside the same module. The variable number and function display() are only accessible from inside the same module.
In Kotlin, the protected modifier strictly allows accessibility to the declaring class and its subclasses. The protected modifier can not be declared at the top level. In the below program, we have accessed the int variable in the getvalue() function of the derived class.
Access Scope:
Example:
Output:
Cannot access 'int': it is protectedWe need to mark the protected variable or function using an open keywords to override in the derived class. In the below program, we have overridden the int variable.
Example:
Output:
The value of integer is: 20By default constructors are public, but we can also change the visibility of a constructor by using the modifiers.
class A (name : String) {
// other code
}
We must explicitly specify this by using the constructor keyword whilst changing the visibility.
class A private constructor (name : String) {
// other code
}