Kotlin Class and Object
Kotlin supports both object oriented programming (OOP) as well as functional
programming. Object oriented programming is based on real
time objects and classes. Kotlin also support pillars of OOP language such as
encapsulation, inheritance and polymorphism.
Kotlin Class
Kotlin class is similar to Java class, a class is a blueprint for the objects which have
common properties. Kotlin classes are declared using keyword class. Kotlin class has
a class header which specifies its type parameters, constructor etc. and the class body
which is surrounded by curly braces.
Syntax of Kotlin class declaration
1. class className{ // class header
2. // property
3. // member function
4. }
In above example, class className is an empty constructor. It is generated by
compiler automatically but if we want to provide a constructor, we need to write a
constructor keyword followed by class name as:
1. class className constructor(){ // class header
2. // property
3. // member function
4. }
Example of Kotlin class
1. class account {
2. var acc_no: Int = 0
3. var name: String? = null
4. var amount: Float = 0f
5.
6. fun deposit() {
7. //deposite code
8. }
9.
10. fun withdraw() {
11. // withdraw code
12. }
13.
14. fun checkBalance() {
15. //balance check code
16. }
17.
18. }
The account class has three properties acc_no, name, amount and three member
functions deposit(), withdraw(),checkBalance().
Pause
Unmute
Current Time 1:06
Duration 4:57
Loaded: 100.00%
Â
Fullscreen
In Kotlin, property must be initialize or declare as abstract. In above class, properties
acc_no initialize as 0, name as null and amount as 0f.
Kotlin Object
Object is real time entity or may be a logical entity which has state and behavior. It
has the characteristics:
o state: it represents value of an object.
o behavior: it represent the functionality of an object.
Object is used to access the properties and member function of a class. Kotlin allows
to create multiple object of a class.
Create an object
Kotlin object is created in two steps, the first is to create reference and then create an
object.
1. var obj1 = className()
Creating multiple object
1. var obj1 = className()
2. var obj2 = className()
Here obj1 and obj2 are reference and className() is an object.
Access class property and member function
Properties and member function of class are accessed by . operator using object. For
example:
1. obj.deopsit()
2. obj.name = Ajay
Let's create an example, which access the class property and member function using .
operator.
1. class Account {
2. var acc_no: Int = 0
3. var name: String = ""
4. var amount: Float = 0.toFloat()
5. fun insert(ac: Int,n: String, am: Float ) {
6. acc_no=ac
7. name=n
8. amount=am
9. println("Account no: ${acc_no} holder :${name} amount :${amount}")
10. }
11.
12. fun deposit() {
13. //deposite code
14. }
15.
16. fun withdraw() {
17. // withdraw code
18. }
19.
20. fun checkBalance() {
21. //balance check code
22. }
23.
24. }
25. fun main(args: Array<String>){
26. Account()
27. var acc= Account()
28. acc.insert(832345,"Ankit",1000f) //accessing member function
29. println("${acc.name}") //accessing class property
30. }
Output:
Account no: 832345 holder :Ankit amount :1000.0
Ankit