Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Visibility


The visibility access rules we have discussed for fields apply to properties as well. Therefore, you can have private, protected, or public (default) properties. Furthermore, the setter can have different, more restrictive visibility than the getter (the getter code is generated  for you automatically in the following case):

    class WithPrivateSetter(property: Int) { 
      var SomeProperty: Int = 0 
        private set(value) { 
          field = value 
        } 
 
      init { 
        SomeProperty = property 
      } 
    } 
 
    val withPrivateSetter = WithPrivateSetter(10) 
    println("withPrivateSetter:${withPrivateSetter.SomeProperty}") 

There are scenarios when properties are subject to class inheritance. If this happens, typically protected visibility, at least for the setter, is more appropriate:

    open class WithInheritance { 
      open var isAvailable: Boolean = false 
        get() =...