Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin Quick Start Guide

You're reading from   Kotlin Quick Start Guide Core features to get you ready for developing applications

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789344189
Length 178 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Marko Devcic Marko Devcic
Author Profile Icon Marko Devcic
Marko Devcic
Arrow right icon
View More author details
Toc

Properties

A Kotlin class doesn't have setter or getter methods, yet it doesn't break encapsulation by exposing private fields. The reason is that Kotlin has properties. C# is one of the languages Kotlin drew inspiration from, and the concept of properties is one of the things it took from C#. You can think of properties as fields and methods, all in one place. To the outside users of a property, the syntax looks like they are accessing a public field of a class. Internally in your class, behind a property, you can have get and set accessors or methods and a private backing field. The get and set methods then control how your private field is accessed, thus not breaking encapsulation.

Specifying get and set, as in our case, is optional. If you omit them, then it is implied that the property is both read and write. The compiler generates a private backing field for you...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime