Skip to content
Related Articles
Open in App
Not now

Related Articles

Kotlin Class and Objects

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 08 Feb, 2023
Improve Article
Save Article

In Kotlin, classes and objects are used to represent objects in the real world. A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or fields), and implementations of behavior (member functions or methods).

An object is an instance of a class and has its own state and behavior. You can create multiple objects from the same class, each with its own unique state.

Here is an example of a class in Kotlin:

Kotlin




class Car {
    var make: String
    var model: String
    var year: Int
 
    fun getInfo(): String {
        return "$make $model, year $year"
    }
}
 
fun main() {
    val myCar = Car()
    myCar.make = "Toyota"
    myCar.model = "Camry"
    myCar.year = 2020
 
    println(myCar.getInfo())
}

Output:

Toyota Camry, year 2020 

Kotlin supports both functional and object-oriented programming. In previous articles, we have learned about functions, higher-order functions, and lambdas which represent Kotlin as a functional language. Here, we will learn about the basic OOPs concepts which represent Kotlin as an Object-Oriented programming language. 

Object-Oriented Programming Language 

Class and Objects are the basic concepts of object-oriented programming language. These support the OOPs’ concepts of inheritance, abstraction, etc. 

Class

Like Java, class is a blueprint for objects having similar properties. We need to define a class before creating an object and the class keyword is used to define a class. The class declaration consists of the class name, class header, and class body enclosed with curly braces. 

Syntax of the class declaration:  

class className {      // class header
   // property
   // member function
}
  • Class name: every class has a specific name 
  • Class header: header consists of parameters and constructors of a class 
  • Class body: surrounded by curly braces, contains member functions and other property 

Both the header and the class body are optional; if there is nothing in between curly braces then the class body can be omitted.  

class emptyClass

If we want to provide a constructor, we need to write a keyword constructor just after the class name. 

Creating constructor:  

class className constructor(parameters) {    
   // property
   // member function
}

Example of Kotlin class: 

Kotlin




class employee {
    // properties
    var name: String = ""
    var age: Int = 0
    var gender: Char = 'M'
    var salary: Double = 0.toDouble()
    
      // member functions
       fun name(){
 
    }
    fun age() {
 
    }
    fun salary(){
 
    }
}

Object

It is a basic unit of Object-Oriented Programming and represents the real-life entities, which have states and behavior. Objects are used to access the properties and member functions of a class. In Kotlin, we can create multiple objects of a class. An object consists of:

  • State: It is represented by the attributes of an object. It also reflects the properties of an object. 
  • Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects. 
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.
     

Create an object

We can create an object using the reference of the class. 

var obj = className()

Accessing the property of the class:

We can access the properties of a class using an object. First, create an object using the class reference and then access the property. 

obj.nameOfProperty

Accessing the member function of the class:

We can access the member function of the class using the object. 

obj.funtionName(parameters)

Kotlin program of creating multiple objects and accessing the property and member function of class:

Kotlin




class employee {
   
      // Constructor Declaration of Class
 
    var name: String = ""
    var age: Int = 0
    var gender: Char = 'M'
    var salary: Double = 0.toDouble()
 
    fun insertValues(n: String, a: Int, g: Char, s: Double) {
        name = n
        age = a
        gender = g
        salary = s
        println("Name of the employee: $name")
        println("Age of the employee: $age")
        println("Gender: $gender")
        println("Salary of the employee: $salary")
    }
     
    fun insertName(n: String) {
        this.name = n
    }
 
}
fun main(args: Array<String>) {
    // creating multiple objects
    var obj = employee()
     
    // object 2 of class employee
    var obj2 = employee()
 
    //accessing the member function
    obj.insertValues("Praveen", 50, 'M', 500000.00)
 
    // accessing the member function
    obj2.insertName("Aliena")
 
    // accessing the name property of class
    println("Name of the new employee: ${obj2.name}")
 
}

Output:  

Name of the employee: Praveen
Age of the employee: 50
Gender: M
Salary of the employee: 500000.0
Name of the new employee: Aliena

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!