Kotlin Class and Object

17. Kotlin Class and Object 17.1 Kotlin OOPS 17.2 Kotlin Class 17.3 Kotlin Object 17.4 Access class property and function 17.5 Kotlin init block


Kotlin OOPS

The object oriented programming provides an approach to eliminate the flaws encountered in the procedural approach. Kotlin supports both functional and object oriented programming.

The main purpose of Object Oriented Programming is to simplify the design, programming and most importantly debugging a program. If some piece of codes is written in multiple files, then instead of writing them again and again, it would be better to write in one place and call them when required.

Kotlin Class

A class is a collection of objects of similar type. For example, car, bus, bicycle and truck are members of the class vehicle. Once a class has been defined, we can create any number of objects belonging to that class. Classes are the main building blocks of any object oriented programming language. It describes the data and the behavior of a type and enables you to create your own type of grouping together methods and variables of other types.

The class definition begins with the keyword class and followed by a class name and surrounded by the curly braces.

class school {
}

We can also mention the access specifier keyword before the class keyword. If it is not specified, by default it will be public.

protected class school {
}

By default classes in kotlin are final, means not inheritable.

public final class school {
}

The open keyword is used to make the class inheritable.

open class school {
}

If the class has no body, we can omit the curly braces.

class school 

Kotlin Class Example

class school {
	val name = ''
	
	fun attendence() {
	
	}
}




Kotlin Object

An object is an instance of the class. In object, data members, and properties are bundled together into a single entity. A single application can contain one or many numbers of objects.

class school {
    var roll_no:Int = 0
    var name:String = ""
    fun student(rn: Int, n: String) {
        roll_no = rn
        name = n
        println("Roll No: ${roll_no} and Name: ${name}")
    }
}
fun main(args : Array<String>)  { 
    var obj1 = school()
	var obj2 = school()
    obj1.student(101,"Robert")
	obj2.student(102,"Smith")
}
Output

Roll No: 101 and Name: Robert
Roll No: 102 and Name: Smith

In the above example, we have created two objects of class school. Both obj1 and obj2 are references of the class school.


Access class property and function

The given example demonstrates how to access the class members and properties.

class school {
	var roll_no:Int = 0
	var name:String = ""
	fun student(rn: Int, n: String) {
		roll_no = rn
		name = n
        println("Roll No: ${roll_no} and Name: ${name}")
	}
}
fun main(args : Array<String>)  { 
	var obj1 = school()
	obj1.student(101,"Robert")
	println("${obj1.name}")
}

Roll No: 101 and Name: Robert
Robert

Kotlin init block

The code inside the init block is executed everytime when the class is instantiated.

class checkInit() {
    init {
        println("Our Mission - ")
    }
    init {
        println("About school - ")
    }
}
fun main(args : Array<String>)  { 
    checkInit()
}
Our Mission - 
About school -






Read more articles