In this tutorial you will learn about the Kotlin Class and Objects and its application with practical example.
Kotlin Class and Objects
Kotlin Langauge supports both procedural programming and object-oriented programming. It supports procedural programming with the use of functions.
In object-oriented programming langauge we can divide a complex problems into smaller sets with the help of objects. Object has two characteristics: state and behavior.
Let’s take a real world example, Dogs have state(name,color,breed,hungry) and behavior(barking,fetching,wagging tail).
How to use Function:
1 2 3 4 |
fun main(args: Array<String>) { val scope = "world" println("Hello, $scope!") } |
Kotlin Class: Class is a group of object which have common properties. It is a blueprint of objects. It’s a logical entity .
Define a Class in Kotlin:
1 2 3 4 |
class NameOfClass { // property // member function } |
Example: Let’s get understand it by an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class User { // property (data member) private var isActive: Boolean = false // member function fun Active() { isActive = true } // member function fun inActive() { isActive = false } } |
So here is the Name of Class User, The Property of class is isActive is a type of boolean(can be true or false), It have two member function that are Active() and inActive().
Visibility Modifiers: Kotlin provides a number of Visibility Modifiers that can be used in classes, objects, properties, member function etc. In Above example I have used private visibility modifiers for isActive Property , it means it can use only within the class.
There are four visibility modifiers that are:
1.private: This modifiers can access or visible only inside the class.
2.public: This modifiers can access or visible everywhere.
3.protected: This modifiers can access or visible to the class and its subclass.
4.internal: It is accessble inside the module.
How to use Object in Kotlin: The specification for the Object is defined when a class is defined, We need to create Objects for accessing members which are defined within the class. See How we can use it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class User { // property (data member) private var isActive: Boolean = false // member function fun Active() { isActive = true } // member function fun inActive() { isActive = false } } fun main(args: Array<String>) { val U1 = User() // create U1 object of User class val U2 = User() // create U2 object of User class } |
Here U1 and U2 are the two Objects of User class.
How to Access Member : Now let’s see how can we access the member functions, check below syntax.
U1.Active()
Second Way to access it.
U2.isActive = true
Complete Example : Now let’s see complete example of use of class, objects and modifiers in Kotlin. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class User { // property (data member) private var isActive: Boolean = false // member function fun Active() { isActive = true } // member function fun inActive() { isActive = false } fun printUserStatus(user: String) { if (isActive == true) println("$user W3Adda : user is Active.") else println("$user W3Adda : user is Inactive.") } } fun main(args: Array<String>) { val U1 = User() // create U1 object of User class val U2 = User() // create U2 object of User class U1.Active() U2.inActive() U1.printUserStatus("U1") U2.printUserStatus("U2") } |
Output:The output of above code will print the user status using “printUserStatus” function. Will look like below image.