I'm not sure about the difference between the following two constructors written in Kotlin
class ConcreteItem(val title: String) : Item() {
}
and
class ConcreteItem(private val title: String) : Item() {
}
As you can see, the only difference is the "private" modifier before the title field. How will it affect the scope of the title field?
The following code:
class ConcreteItem(val title: String) : Item() {
}
is a shortcut for:
class ConcreteItem(title: String) : Item() {
val title: String = title
}
where lack of explicit access modifier for val title
implies public
:
class ConcreteItem(title: String) : Item() {
public val title: String = title
}
Similarly, the following code:
class ConcreteItem(private val title: String) : Item() {
}
is a shortcut for:
class ConcreteItem(title: String) : Item() {
private val title: String = title
}
Taking that in account, the difference between two declarations is simply declaring public property vs private property.
if the private
modifier is applied, then a public "getter" won't be synthesized whereby other classes can interrogate the value of "title". otherwise will be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With