Kotlin Class and Objects
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:
class Car {
var brand: String = ""
var model: String = ""
var year: Int = 0
fun getInfo(): String {
return "$brand $model, year $year"
}
}
fun main() {
val myCar = Car()
myCar.brand = "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.
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which are instances of classes. These objects contain state (properties) and behavior (functions or methods). Kotlin supports key OOP concepts such as:
- Classes and Objects
- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
Kotlin Class
Similar to Java, a Class in Kotlin 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 {
// properties
// member functions
}
- Class name: Unique name for the class.
- Class header: Can include constructors and parameters.
- Class body: Enclosed in {}, contains logic, properties, and functions.
If the class body is empty, we can omit the curly braces:
class EmptyClass
If we want to provide a constructor, we need to write a keyword constructor just after the class name.
Example of Kotlin class:
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(){ }
}
Creating a Class with a Constructor -
We can use a constructor in Kotlin right after the class name:
class ClassName constructor(parameters) {
// property
// member function
}
Or, we can also use a primary constructor as shown below:
class ClassName (parameters) {
// property
// member function
}
Example of Kotlin class with constructor:
class Car(val brand: String, val model: String, val year: Int) {
fun getInfo() {
println("$brand $model, year $year")
}
}
Kotlin 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: properties (e.g., name, age)
- Behavior: methods/functions (e.g., run(), walk())
- Identity: a reference (variable name) to distinguish it from other objects
Creating an Object
We can create an object using the reference of the class.
var obj = className()
for eg. here we are calling the class "Car" while assigning parameters.
val car1 = Car("Toyota", "Camry", 2020)
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)
for eg.
car1.getInfo()
Output:
Toyota Camry, year 2020
Kotlin program of creating multiple objects and accessing the property and member function of class:
class Employee(val name: String, val age: Int, val gender: Char, val salary: Double) {
fun showDetails() {
println("Name of the employee: $name")
println("Age of the employee: $age")
println("Gender of the employee: $gender")
println("Salary of the employee: $salary")
}
}
fun main() {
val emp1 = Employee("Praveen", 50, 'M', 500000.0)
emp1.showDetails()
val emp2 = Employee("Aliena", 30, 'F', 400000.0)
println("Name of the new employee: ${emp2.name}")
}
Output:
Name of the employee: Praveen
Age of the employee: 50
Gender of the employee: M
Salary of the employee: 500000.0
Name of the new employee: Aliena