Visibility Modifier
The visibility modifier basically defines a scope from where it can be visible and accessible. There are four visibility modifiers provided by the Kotlin.
- private
- protected
- internal
- public
The default visibility modifier is public.
Note: We can't use the visibility modifier with Local variables, Local functions, and Local classes. Here is the code snippet that is self-explanatory:
private class Number() {
private fun add() {
// Can't use visibility Modifier here with class declaration
private class numberFloat{...}
// Can't use visibility Modifier here with local function
private fun addFloat() {...}
}
}
Private:
The scope of the private modifier is the containing class only. It's visible and accessible only inside the file/ class where it is declared. The code snippet explain it more:
TestPrivate.kt file
// Visible and accessible only inside this file
private const val two: Int = 2
// Visible and accessible only inside this file
private class Number() {
// Visible and accessible only inside the Number class
private val seven = two + 5
}
// ERROR: seven is not accessible outside the Number class
private const val nine = seven + two
Protected:
The scope of the protected modifier is the containing class along with its sub-classes, no matter where these sub-classes are declared. If you have used java before, that even does not allow it in the different packages.
Note: This modifier is not allowed for top-level declarations and inside the object class.
Here is a code snippet that will show the usage of the protected modifier.
TestProtected.kt file
TestProtected.kt file
// ERROR: protected modifier is not allowed at top-level declarations
protected const val three: Int = 3
// ERROR: protected modifier is not allowed at top level declarations
protected class HelloWorld(){...}
// Visible and accessible only inside this file
private const val two: Int = 2
// Visible and accessible only inside this file
private open class Number {
// Visible and accessible only inside the Number class and it's subclasses
protected val seven = two + 5
}
// Visible and accessible only inside this file
private class NumberFloat : Number() {
// seven is accessible because NumberFloat is a subclass of Number
private val num: Float = two + 7.5f + seven
}
// ERROR: num and seven is not accessible here
private val test = num + seven
Internal:
The scope of the internal modifier is the module where the declaration file exist. It'll not be visible to any other module even if the declaration file is accessible to that module.
A module is a bunch of Kotlin files that compiled together, like:
A module is a bunch of Kotlin files that compiled together, like:
- An IntelliJ IDEA project or module
- A Maven project or module
- A Gradle project or module
For Example:
If we have a gradle project and we have two module as presentation module and data module in the project. If we have AppConstant.kt in presentation module and ApiConstant.kt in data module. If we'll try to access internal variable from ApiConstant.kt to AppConstant.kt then it will not be accessible, even If ApiConstant.kt is accessible in presentation module. Here is the code snippet that will clear the things more:
ApiConstant.kt file in data module
object ApiConstant {
internal const val PARAM_USERNAME = "username"
}
AppConstant.kt file in presentation module
object AppConstant {
// PARAM_USERNAME is not accessible in different module
const val USERNAME = ApiConstant.PARAM_USERNAME
}
Public:
The public modifier is accessible to code where the declaration class/ file is accessible. The default visibility modifier in Kotlin is public. It is like internal modifier with some extra accessibility. In the above example if PARAM_USERNAME would be public then it would be accessible to AppConstant.kt. Here is the code snippet:
ApiConstant.kt file in data module
object ApiConstant {
// public is default visibility modifier so we can remove it.
public const val PARAM_USERNAME = "username"
}
AppConstant.kt file in presentation module
object AppConstant {
// PARAM_USERNAME is accessible in different module
const val USERNAME = ApiConstant.PARAM_USERNAME
}
I think we have taken enough knowledge about visibility modifier in Kotlin now.
Happy Learning!!
No comments:
Post a Comment