I started developing Android apps in Java where encapsulation of object-oriented programming was achieved through declaring variables as private fields with their getter and setter as public methods. The moment I converted my Java code to Kotlin, it replaced each variable along with its getter and setter with just a single line of code. Although I was amazed at how can a single line of code replace the complete variable with the same functionality, but later on understanding it, I started liking writing the code in Kotlin. Let’s understand how it works in Kotlin.
Variable having a class level scope (member variables) which are declared inside the class but outside the methods or functions is called as Property
in Kotlin. They are declared with var
keyword for a mutable one and with val keyword for a read-only/nonmutable one.
The full syntax for declaring a property is
var <propertyName>: <PropertyType> = <property_initializer>
[<getter>]
[<setter>]
The initializer, getter and setter are optional.
val name : String = "John"
var age : Int = 0
which is exactly the replacement of
public final String name = "John";
public int age = 0;
Things to notice
- No need to add
public
modifier as by default, all properties and functions are public in Kotlin. Addprivate
orprotected
if required. - A property must be initialized or need to be declared as
abstract
in Kotlin . Instead, we can even uselateinit
which is better explained here. - All properties are
final
by default. If you want to override some property or its getter setter, define the property asopen
(explained later in the same post).
But we just broke the rule of encapsulation by defining a variable as public
. What if I want to declare the variable as private
having a getter and a setter as public
methods.
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Here we go.
var name: String = “John”
Where are my getter and setter?
Trust me, they are there. No need to define them explicitly.
Ok, I Trust you. What if I want to override the getter and setter.
private int size = 0;
private boolean isEmpty;
public boolean isEmpty() {
return size == 0;
}
absolutely simple.
private val size : Int = 0
var isEmpty: Boolean = false
private set
get () = size == 0
Please note that the setter is set private
here as it can’t be set from outside the class.
This was an easy one. Let me try something complicated.
what about this.
private String firstName;
private String lastName;
//private String name;
public String getName() {
return firstName + " " + lastName;
}
public void setName(String name) {
String nameArray[] = name.split(" ");
firstName = nameArray[0];
lastName = nameArray[1];
}
I have three private
variables firstName, lastName and name but only getter setter of name variable which does break the name into two parts and store them in firstName and lastName respectively in case of the setter.
Also, in case of the getter, it returns the firstName and lastName with a space between them.
What will be the equivalent Kotlin code for this?
Well, it’s not that difficult.
private var firstName: String? = null
private var lastName: String? = null
var name: String
get() = firstName + " " + lastName
set(value) {
val nameArray = value.split(" ".toRegex())
firstName = nameArray[0]
lastName = nameArray[1]
}
Ok. Few questions
1. How can I make setter private?
Add private
keyword before set.
public var name: String = "John"
private set
2. How can I override the getter or setter in an extending class?
Define the variable as open
in the base class and use override
keyword in the extending class.
//Base class
open var age: Int = 0
get() = 10
//Extending class
override var age: Int = 0
get()= 20