EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Classes and Object

In Kotlin, classes and objects are key constructs to represent real-world entities. A class serves as a blueprint for creating objects, defining their structure (properties) and behavior (functions). Each object created from a class holds its own state and behavior. Multiple objects can be instantiated from a single class, each having unique values.

A class in Kotlin may define properties and methods, which can later be accessed by creating objects of that class.

Example of a Kotlin Class:

class Animal {
    var name: String = ""
    var type: String = ""
    var age: Int = 0

    fun getDetails(): String {
        return "Animal: $name, Type: $type, Age: $age years"
    }
}

fun main() {
    val myPet = Animal()
    myPet.name = "Bella"
    myPet.type = "Dog"
    myPet.age = 3

    println(myPet.getDetails())
}

Output:

Animal: Bella, Type: Dog, Age: 3 years

Object-Oriented Programming in Kotlin:

Kotlin combines both functional and object-oriented paradigms. Earlier, we’ve explored functional aspects such as higher-order functions and lambdas. Now, let’s dive into the object-oriented nature of Kotlin.

Core OOP Concepts in Kotlin:

  • Class: A class acts as a blueprint for objects, defining similar properties and methods. In Kotlin, classes are declared using the class keyword. Syntax:
class ClassName {  // class header
    // properties
    // member functions
}
  • Class Name: Every class must have a name.
  • Class Header: Includes parameters and constructors.
  • Class Body: Enclosed by curly braces {}, containing properties and methods. Both the class header and body are optional, and the body can be omitted if empty.
class EmptyClass

Creating a Constructor:

class ClassName constructor(parameters) {
    // properties
    // member functions
}

Example of a Kotlin Class with Constructor:

val obj = ClassName()

Output:

class Employee {
    var name: String = ""
    var age: Int = 0
    var gender: Char = 'M'
    var salary: Double = 0.0

    fun setDetails(n: String, a: Int, g: Char, s: Double) {
        name = n
        age = a
        gender = g
        salary = s
    }

    fun displayInfo() {
        println("Employee Name: $name")
        println("Age: $age")
        println("Gender: $gender")
        println("Salary: $salary")
    }
}

Objects in Kotlin:

An object is an instance of a class, allowing access to the class properties and methods. You can create multiple objects from a class, each representing unique instances.

  • State: Defined by object attributes (properties).
  • Behavior: Defined by object methods.
  • Identity: Each object has a unique identity allowing interaction with other objects.

Creating an Object:

val obj = ClassName()

Accessing Class Properties:

obj.propertyName

Accessing Class Methods:

obj.methodName()

Accessing Class Properties:

obj.methodName()

Kotlin Program with Multiple Objects:

class Employee {
    var name: String = ""
    var age: Int = 0
    var gender: Char = 'M'
    var salary: Double = 0.0

    fun setDetails(n: String, a: Int, g: Char, s: Double) {
        name = n
        age = a
        gender = g
        salary = s
    }

    fun setName(n: String) {
        this.name = n
    }

    fun displayDetails() {
        println("Employee Name: $name")
        println("Age: $age")
        println("Gender: $gender")
        println("Salary: $salary")
    }
}

fun main(args: Array<String>) {
    // Creating multiple objects of Employee class
    val emp1 = Employee()
    val emp2 = Employee()

    // Setting and displaying details for the first employee
    emp1.setDetails("John", 35, 'M', 55000.0)
    emp1.displayDetails()

    // Setting and displaying name for the second employee
    emp2.setName("Emily")
    println("Second Employee Name: ${emp2.name}")
}

Output:

Employee Name: John
Age: 35
Gender: M
Salary: 55000.0
Second Employee Name: Emily
End of lesson.