Junior — Middle
What are the levels of access restriction properties and methods in Data Class in Kotlin?
sobes.tech AI
Answer from AI
In Kotlin, data classes have standard access levels such as public, private, protected, and internal, which can be applied to properties and methods.
By default, all properties and methods in a data class have public access, meaning they are accessible from anywhere.
Example:
data class User(val name: String, private val password: String) {
fun getMaskedPassword(): String {
return "****"
}
}
Here, name is a public property accessible everywhere, while password is private, accessible only within the class. Methods can also have different access levels.
Thus, access levels in data classes are the same as in regular Kotlin classes and allow controlling the visibility of properties and methods.