https://kotlinlang.org logo
Title
p

Prateek

10/01/2017, 6:55 PM
@karelpeeters do you agree that by default the var/val in a kotlin class is public?
k

karelpeeters

10/01/2017, 6:58 PM
Yes. The backing JVM field, however, is not.
You can see this for yourself: try to access the properties of a data class from Java.
p

Prateek

10/01/2017, 6:59 PM
I agree but this has nothing to with the java interop my question is strictly about the kotlin design choice
if a property is public isnt it violating the core OOP principle?
k

karelpeeters

10/01/2017, 7:02 PM
Even in Kotlin, the backing property is not public. For example,
foo.bar = 5
is compiled down to
foo.setBar(5)
. Notice how you're not actually using a public property? Trying to call Kotlin from Java just makes this distinction clear.
p

Prateek

10/01/2017, 7:03 PM
Aaah makses sense so even though the property is public, you are only accessing it via the implied getters and setters correct?
k

karelpeeters

10/01/2017, 7:03 PM
That's right.
p

Prateek

10/01/2017, 7:04 PM
Awesome... thanks for clearing it @karelpeeters I appreciate the help 😄
k

karelpeeters

10/01/2017, 7:04 PM
The backing JVM property isn't actually public of course, as that would indeed break encapsulation.
No problem, glad I could help!
🙌 2