The basic and most important idea of a class is the Encapsulation. It is a property to encapsulate code and data, into a single entity. In Java, the data are stored in the fields and these are mostly private.
So, accessor methods – a getter and a setter are provided to let the clients of the given class access the data. For sending notifications about the change or validating the passed value, additional logic is also present in the setter.
Property –
It is the combination of accessories and the fields in case of Java. In case of Kotlin, properties are meant to be a first-class language feature. These features replace fields and accessor methods. A property in a class is declared the same as declaring a variable with val and var keywords. A property declared as var is mutable and thus, can be changed
Defining a class :
class Abc( val name: String, val ispassed : Boolean ) |
Readable property – generates a field and a trivial getter
Writable property – a getter, a setter and a field
Basically what happens is that the declaration of the property declares the related accessors (both setter and getter for writable and getter for readable property). A field stores the value
Let’s see the class usage
class Abc( val name: String, val ispassed : Boolean ) fun main(args: Array<String>) { val abc = Abc("Bob",true) println(abc.name) println(abc.ispassed) /* In Java Abc abc = new Abc("Bob",true); System.out.println(person.getName()); System.out.println(person.isMarried()); */} |
Output :
Bob true
In Kotlin, the constructor can be called without new keyword. Instead of invoking the getter, the property is referenced directly. The logic remains the same and the code is much more concise. Setters of mutable property works in a similar manner.
Customer Accessors
Custom implementation of property accessor.
class Rectangle(val height: Int, val width: Int) { val isSquare: Boolean get() { return height == width } } fun main(args: Array<String>) { val rectangle = Rectangle(41, 43) println(rectangle.isSquare) } |
Output :
false
The property isSquare need no field to store the value. It only has a custom getter with the implementation provided. Every time the property is accessed, the value is computed .
Recommended Posts:
- Delegated Properties in Kotlin
- How to add a custom styled Toast in Android using Kotlin
- How to Create a Dark Mode for a Custom Android App in Kotlin?
- Kotlin Nested class and Inner class
- Kotlin Class and Objects
- Kotlin Abstract class
- Singleton Class in Kotlin
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin Expression, Statement and Block
- Kotlin | Default and Named argument
- Kotlin | Lambdas Expressions and Anonymous Functions
- Kotlin Setters and Getters
- Kotlin Nested try block and multiple catch block
- Kotlin | Type Checking and Smart Casting
- Kotlin | Plus and minus Operators
- Difference between Kotlin and Scala
- Difference between Java and Kotlin in Android with Examples
- Difference between var and val in Kotlin
- How to Implement Tabs, ViewPager and Fragment in Android using Kotlin?
- Kotlin Data Types
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : panwaravishkar

