Object Oriented Programming Kotlin (Part:04)
OOP in kotlin is one of the most important topic in kotlin language. We will refactoring our code, in this part of this tutorial. If you see our below code we refactor our code in OOP way but line is not reduce. So we need more refactoring our code.
Another note: Create object in Kotlin no need to write new keyword .
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// var johnFirstName = "MD"
// var johnMiddleName = "John"
// var johnLastName = "Ahamed"
//
// var johnFullName = johnFirstName + " "+ johnMiddleName + " "+ johnLastName
// var johnReverseName = johnLastName+ " "+ johnMiddleName + " "+ johnFirstName
var john = Employee()
john.firstName = "Md"
john.middleName = "John"
john.lastName = "Ahamed"
println(john.getFullName())
println(john.getReverseName())
|
We set our employee’s field value when the object is born. Every object oriented programming language when a object is born a special method is called which name is constructor . If you not write any constructor in your code a default constructor is created automatically . If you again see out folder Employee file
1
2
3
4
5
6
7
8
9
10
11
|
public final class Employee public constructor() {
public final var firstName: kotlin.String /* compiled code */
public final var lastName: kotlin.String /* compiled code */
public final var middleName: kotlin.String /* compiled code */
public final fun getFullName(): kotlin.String { /* compiled code */ }
public final fun getReverseName(): kotlin.String { /* compiled code */ }
}
|
you see constructor() is created in first file of byte code. It is the default constructor.
Default constructor : a constructor which has no signature or parameter.
we need to crate a constructor which store employee first , middle and last name when object is born . So we
need to modify our Employee class with a constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Employee{
constructor(firstName: String,middleName:String,lastName:String){
this.firstName = firstName
this.middleName = middleName
this.lastName = lastName
}
var firstName ="" // has value
var middleName = "" // has value
var lastName = "" // has value
fun getFullName(): String { // does work
return firstName + " "+ middleName + " "+ lastName
}
fun getReverseName():String{ //does work
return this.lastName + " "+ this.middleName + " "+ this.firstName
}
}
|
here Employee Class we write a constructor . now
1
2
3
4
5
6
7
8
9
10
11
|
// var john = Employee()
// john.firstName = "Md"
// john.middleName = "John"
// john.lastName = "Ahamed"
// println(john.getFullName())
// println(john.getReverseName())
var john = Employee("Md","John","Ahamed")
println(john.getFullName())
println(john.getReverseName())
|
now see 6 line of code is replace by only 3 line . But you uncomment first part of this code create error cause if you write any constructor default constructor will removed.
after clicking run button if you again see the out folder Employee file you see the difference (default constructor is removed)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public final class Employee {
public constructor(firstName: kotlin.String, middleName: kotlin.String, lastName: kotlin.String) { /* compiled code */ }
public final var firstName: kotlin.String /* compiled code */
public final var lastName: kotlin.String /* compiled code */
public final var middleName: kotlin.String /* compiled code */
public final fun getFullName(): kotlin.String { /* compiled code */ }
public final fun getReverseName(): kotlin.String { /* compiled code */ }
}
|
now compare both of the bitcode version of Employee Class you see the differences.
Object Oriented Programming in kotlin links:
- Object Oriented Programming Kotlin (Part:04)
- Object Oriented Programming Kotlin (Part:03)
- Object Oriented Programming Kotlin (Part:02)
- Object Oriented Programming Kotlin (Part:01)
Kotlin language supports OOP (Object Orientated Programming). There are four terms in OOP. The first term is encapsulation, second is abstraction, third inheritance and finally fourth is polymorphism
Next part of this tutorial series we will learn what is encapsulation in Kotlin.
Happy Coding!!!