Object Oriented Programming Kotlin (Part:04)

By | October 14, 2017

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 .

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

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.

 

here Employee Class we write a constructor . now

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)

 

now compare both of the bitcode version of Employee Class you see the differences.

Object Oriented Programming in kotlin links:

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!!!

Leave a Reply