Quite similar to that of Java.
Okay, folks! We’ll start learning about Kotlin’s OOP today. But don’t worry, the Android=centric language’s OOP is similar to Java. So, if you already know Java, like me, you probably won’t have any problems learning the OOP concepts of it.
Hi all, it’s been three days since I last posted here… Well, to be honest, I lost some motivation to keep up with my coding series here since I’m more inclined to do my own personal projects. BUT, I’ll start posting things here too. At least, I have to finish up Kotlin’s basic learning processes to start my own asap.
Anyways, let’s go!!
Kotlin’s OOP:
This is our first Kotlin’s OOP programming. As I assume that you can understand the basics of Java or other programming languages, I skip explaining this.
classSample1.kt
class Car() {
val color : String = "blue"
}
fun main() {
val car = Car()
println(car.color)
}
$java -jar classSample1.jar
blue
And I added a function.
classSample1.kt
class Car() {
val color : String = "blue"
fun drive() {
println("VMMMMMMMMMMMMMM!!!")
}
}
fun main() {
val car = Car()
println(car.color)
println(car.drive())
}
$java -jar classSample1.jar
blue
VMMMMMMMMMMMMMM!!!
kotlin.Unit
And now the Car class receives arguments.
classSample1.kt
class Car(color : String = "blue", model : String = "XMD") {
var color : String = color
var model : String = model
}
fun main() {
val car = Car(color = "RED", model = "TOYOTA")
println(car.color)
println(car.model)
}
$java -jar classSample1.jar
RED
TOYOTA
Init block:
Okay, so from here, we’ll look a bit deeper into Kotlin’s world. Inti block is a block of code that is executed when an object is created, similar to Java’s constructor. Here’s an example.
classSample1.kt
class Car(var color : String, var model : String) {
init {
color = "Yellow"
model = "Lexus"
}
}
fun main() {
val car = Car(color = "RED", model = "TOYOTA")
println(car.color)
println(car.model)
}
$java -jar classSample1.jar
Yellow
Lexus
Inheritance:
classSample1.kt
Inheritance is one of the core concepts of OOP, isn’t it? As you may be already aware of this, what is a powerful feature of inheritance that can be seen in the below code is the fact that Truck’s object can access to Car’s functions, drive, and speedMeter.
open class Car(var color : String, var model : String) {
init {
color = "Yellow"
model = "Lexus"
}
fun drive() {
println("VMMMMMMMMMMMMM")
}
fun speedMeter(minSpeed : Int, maxSpeed : Int) {
println("drive: " + minSpeed + " " + maxSpeed)
}
}
class Truck(color : String, model : String) : Car (color, model) {
fun lightUp() {
println("Pika Pika")
}
}
fun main() {
val car = Car(color = "RED", model = "TOYOTA")
println(car.color)
println(car.model)
val truck = Truck(color = "White", model = "Suzuki")
println(truck.lightUp())
println(truck.drive())
println(truck.speedMeter(20, 30))
}
$java -jar inheritanceSample1.jar
Yellow
Lexus
Pika Pika
kotlin.Unit
VMMMMMMMMMMMMM
kotlin.Unit
drive: 20 30
kotlin.Unit
Override:
Okay, this is another OOP feature that doesn’t need any explanation. But there, as you can see Truck’s drive function overrides the original one.
classSample1.kt
open class Car(var color : String, var model : String) {
init {
color = "Yellow"
model = "Lexus"
}
open fun drive() {
println("VMMMMMMMMMMMMM")
}
fun speedMeter(minSpeed : Int, maxSpeed : Int) {
println("drive: " + minSpeed + " " + maxSpeed)
}
}
class Truck(color : String, model : String) : Car (color, model) {
fun lightUp() {
println("Pika Pika")
}
override fun drive() {
println("VMMMMMMMMMMMMM ... like a truck")
}
}
fun main() {
val car = Car(color = "RED", model = "TOYOTA")
println(car.color)
println(car.model)
val truck = Truck(color = "White", model = "Suzuki")
println(truck.lightUp())
println(truck.drive())
println(truck.speedMeter(20, 30))
}
$ java -jar inheritanceSample1.jar
Yellow
Lexus
Pika Pika
kotlin.Unit
VMMMMMMMMMMMMM ... like a truck
kotlin.Unit
drive: 20 30
kotlin.Unit
Extention function:
This is a Kotlin-only feature that probably can’t be seen in Java, I guess…
extentionFunSample.kt
fun String.append(toAppend: String): String{
return this.plus(toAppend)
}
fun main() {
println("Hello, Jenny".append("Miss"))
}
$ java -jar extentionFunSample.jar
Hello, JennyMiss
extentionFunSample2.kt (shorter version)
And you can make it even shorter.
fun String.append(toAppend: String): String = this.plus(toAppend)
fun main() {
println("Hello, Jenny".append("Miss"))
}
$ java -jar extentionFunSample2.jar
Hello, JennyMiss
Data class
Data class is Kotlin’s equivalent of C’s structure, I guess. Early in the year, I posted about it in the C programming series, so if you have some extra time, please check it out too: Learn C Language – Part 4: Structure.
dataClassSample.kt
fun main() {
var person = Person(
name = "John",
lastName = "Doe",
age = 23
)
println(person)
}
data class Person(val name: String, val lastName: String, val age: Int)
$ java -jar dataClassSample.jar
Person(name=John, lastName=Doe, age=23)
And you can loop through multiple data objects.
dataClassSample2.kt
fun main() {
var person = Person(
name = "John",
lastName = "Doe",
age = 23
)
val person2 = Person(name = "Katy", lastName = "Holms", age = 33)
val personList = listOf(person, person2)
for (i in personList) {
println(i)
}
}
data class Person(val name: String, val lastName: String, val age: Int)
$ java -jar dataClassSample2.jar
Person(name=John, lastName=Doe, age=23)
Person(name=Katy, lastName=Holms, age=33)