Jordan Carlyon
05/24/2022, 3:41 PMvars
?ephemient
05/24/2022, 3:45 PMprivate for subclasseesisn't that "protected"?
Jordan Carlyon
05/24/2022, 3:47 PMCasey Brooks
05/24/2022, 3:48 PMprotected
on the set
method of itephemient
05/24/2022, 3:53 PMpublic var someValue: String = ""
protected set
is equivalent to the definition in the previous imageJordan Carlyon
05/24/2022, 4:06 PMsomeValue
can be modified by anyone thoughCasey Brooks
05/24/2022, 4:15 PMprivate
fields with reflection. internal
is the closest analog Kotlin has to package-private, except that it actually does give strong guarantees around encapsulation in different modules
It’s difficult to truly ensure that a field is never modified except by your own code, so the better route isn’t to forcibly hide those fields, bur rather provide sufficient documentation to let consumers know when they should and should not modify it, so they can make the decision for themselves. Using opt-in annotations on those APIs can help make sure that the library users are aware of the concernsJordan Carlyon
05/24/2022, 4:15 PMCasey Brooks
05/24/2022, 4:17 PMJordan Carlyon
05/24/2022, 4:20 PMephemient
05/24/2022, 4:22 PMget
and set
immediately following a property declaration have special meaning, otherwise they don't. but by convention they are indented to visually mark them as belonging to the propertyCasey Brooks
05/24/2022, 4:22 PMval stringRepresentation: String
is the property). get()
and set(value)
is the specific syntax for customizing the accessors of the property. Anyone accessing the property with myClass.stringRepresentation
will not care whether the property internally uses the get()
or set(value)
accessors or notephemient
05/24/2022, 4:23 PMJordan Carlyon
05/24/2022, 4:26 PMephemient
05/24/2022, 4:28 PMCasey Brooks
05/24/2022, 4:34 PMgetSomeValue()
method because the compiler is already creating a method with that name from the propertyJordan Carlyon
05/24/2022, 4:36 PMephemient
05/24/2022, 4:40 PMJordan Carlyon
05/24/2022, 4:41 PMephemient
05/24/2022, 4:42 PMpublic var property: Type
private set
it sounds like what you want isn't "private", but more like "protected" or "internal"?Jordan Carlyon
05/24/2022, 4:43 PMephemient
05/24/2022, 4:43 PMprotected
, not private
.Jordan Carlyon
05/24/2022, 4:43 PMephemient
05/24/2022, 4:44 PMprivate
is really "file-private", so that does give you some optionsJordan Carlyon
05/24/2022, 4:45 PMephemient
05/24/2022, 4:45 PMJordan Carlyon
05/24/2022, 4:46 PMclass Test {
var topLevel
class subClass {
}
}
Chris Lee
05/24/2022, 4:47 PMclass Foo {
private val onlyVisibleInThisClass : String
protected val alsoVisibleToSubclasses : String
internal val visibleToEverythingInThisModule : String
public val visibleToEverything : String
}
ephemient
05/24/2022, 4:47 PMJordan Carlyon
05/24/2022, 4:49 PMChris Lee
05/24/2022, 4:50 PMclass subClass : Test()
Jordan Carlyon
05/24/2022, 4:52 PMChris Lee
05/24/2022, 4:53 PMJordan Carlyon
05/24/2022, 4:54 PMChris Lee
05/24/2022, 4:54 PMJordan Carlyon
05/24/2022, 4:55 PMChris Lee
05/24/2022, 4:55 PMJordan Carlyon
05/24/2022, 4:55 PMChris Lee
05/24/2022, 4:56 PMephemient
05/24/2022, 4:56 PMJordan Carlyon
05/24/2022, 4:56 PMsealed class SealedClass() {
object Child
}
ephemient
05/24/2022, 4:57 PMChild : SealedClass()
to make it a child. it does not matter if it is nested or not.Casey Brooks
05/24/2022, 4:57 PMChris Lee
05/24/2022, 4:58 PMpublic abstract class BaseClass() {
}
public sealed class Child1 : BaseClass() {
}
public sealed class Child2 : BaseClass() {
}
ephemient
05/24/2022, 4:58 PMChild1
and Child2
are nested inside of BaseClass
or notChris Lee
05/24/2022, 4:59 PMpublic abstract class BaseClass() {
public sealed class Child1 : BaseClass() {
}
public sealed class Child2 : BaseClass() {
}
}
Jordan Carlyon
05/24/2022, 4:59 PMephemient
05/24/2022, 4:59 PMCasey Brooks
05/24/2022, 4:59 PMinner class
keyword to make it like the default Java nested classesephemient
05/24/2022, 5:00 PMJordan Carlyon
05/24/2022, 5:00 PMephemient
05/24/2022, 5:00 PMpublic class Parent {
static class Nested extends Parent {}
class Inner extends Parent {}
}
without extends Parent
, they are only nested, not childrenJordan Carlyon
05/24/2022, 5:01 PMCasey Brooks
05/24/2022, 5:03 PM