"Mastering Kotlin: Private Set, Public Get Access Modifiers"

Mastering Kotlin: Understanding 'private set public get'

The Kotlin programming language, developed by JetBrains, offers a unique feature that allows you to control the accessibility of a property's setter and getter independently. This is achieved through the 'private set public get' syntax. Let's delve into the intricacies of this feature and understand how it can be beneficial in your coding endeavors.

What is 'private set public get' in Kotlin?

'private set public get' is a Kotlin property access modifier combination that restricts the setter to only be accessible within the same class (private) while allowing the getter to be accessible from any other class (public). This provides a level of encapsulation, ensuring that the property's value can only be modified within the class, while still allowing external classes to retrieve its value.

Why use 'private set public get'?

  • Encapsulation: It helps in encapsulating the property, hiding its internal details and protecting it from external interference.
  • Data Validation: By keeping the setter private, you can add custom logic or validation checks when the property's value is changed within the class.
  • Readability: It makes the code more readable and easier to understand, as the getter and setter have different access levels.

Syntax and Usage

The syntax for using 'private set public get' is quite straightforward. Here's an example:

Post by @im-a-developer · 1 image
Post by @im-a-developer · 1 image

```kotlin class User private constructor(var name: String) { val isAdult: Boolean get() = age >= 18 // 'isAdult' is public get, 'name' is private set private set } ```

Accessing Properties with 'private set public get'

Since the getter is public, you can access the property's value from any other class. Here's how you can do it:

```kotlin fun main() { val user = User("John Doe") println(user.isAdult) // Accessing public getter // user.name = "Jane Doe" // Error: Val cannot be reassigned } ```

Changing the Property Value

As the setter is private, you can only change the property's value within the same class. Here's how you can do it:

```kotlin class User private constructor(var name: String) { val isAdult: Boolean get() = age >= 18 fun setAge(age: Int) { if (age >= 0) { this.age = age } } private set } ```

Best Practices

While 'private set public get' offers great control over your properties, it's essential to use it judiciously. Here are some best practices:

playing cards and candles on a table with wine glasses in the foreground at night
playing cards and candles on a table with wine glasses in the foreground at night

  • Use it sparingly. Overusing it can make your code more complex and harder to understand.
  • Consider using data classes or sealed classes for simple data holders. They provide a more straightforward way to control access to properties.
  • Use it when you want to expose a derived property (like 'isAdult' in our example) but don't want to expose the underlying property (like 'age').

Understanding and effectively using 'private set public get' in Kotlin can significantly enhance your coding skills and help you write more secure, maintainable, and efficient code.

two women sitting at a table working on their laptops in an office setting with striped walls and flooring
two women sitting at a table working on their laptops in an office setting with striped walls and flooring
a woman walking down a sidewalk holding a box with some items in it's hand
a woman walking down a sidewalk holding a box with some items in it's hand
a woman with her arms up and two speech bubbles above her head
a woman with her arms up and two speech bubbles above her head
two women standing at a podium in front of flags and other people sitting around tables
two women standing at a podium in front of flags and other people sitting around tables
photographers taking pictures in front of a large group of people
photographers taking pictures in front of a large group of people
a group of people sitting around a table with plates and glasses in front of them
a group of people sitting around a table with plates and glasses in front of them
a group of people sitting at a table drinking wine and talking to each other in a restaurant
a group of people sitting at a table drinking wine and talking to each other in a restaurant
a piece of paper with the words what you do in private shows in public reading shows
a piece of paper with the words what you do in private shows in public reading shows
a group of men in suits and hats standing next to each other with the caption keep it private people love to ruin things
a group of men in suits and hats standing next to each other with the caption keep it private people love to ruin things
a group of people sitting in chairs listening to each other
a group of people sitting in chairs listening to each other
kotlin private set public get
kotlin private set public get
the entrance to the park hotel is decorated with paper towels and magazines hanging on a rack
the entrance to the park hotel is decorated with paper towels and magazines hanging on a rack
What’s the difference between Public, Private and Permissioned Blockchains?
What’s the difference between Public, Private and Permissioned Blockchains?
a long table with many people sitting at it and hanging lights above the tables in front of them
a long table with many people sitting at it and hanging lights above the tables in front of them
the words plans are private written in black and yellow on a blue background with snow
the words plans are private written in black and yellow on a blue background with snow
a group of people sitting around a dinner table
a group of people sitting around a dinner table
a group of people standing around a large cake
a group of people standing around a large cake
an outdoor patio with chairs, table and potted plants in the back yard area
an outdoor patio with chairs, table and potted plants in the back yard area
a long dining table with place settings and wine glasses on it in a dimly lit room
a long dining table with place settings and wine glasses on it in a dimly lit room
a man standing in front of a stage with white chairs and a sign on the wall behind him
a man standing in front of a stage with white chairs and a sign on the wall behind him
an outdoor dining area with tables and chairs covered in greenery, lit by candles
an outdoor dining area with tables and chairs covered in greenery, lit by candles
three different shots of people sitting on couches in front of a building
three different shots of people sitting on couches in front of a building
a man sitting on the ground wearing a suit and tie
a man sitting on the ground wearing a suit and tie