Kotlin Classes and Objects : Let’s define a Class
Kotlin is a modern and powerful programming language that helps developers work with object-oriented programming (OOP). In OOP, classes are important because they provide a blueprint for creating objects. In this guide, we will explore Kotlin classes and learn about their syntax, features, and how they contribute to creating strong and easy-to-maintain code.
Along the way, we will cover various topics including class declarations, properties, constructors, member functions, inheritance, and more. By the end of this guide, you will have a clear understanding of Kotlin classes and be prepared to build flexible and reusable code structures.
Class Declarations and Instantiation in Kotlin
How to declare a class and instantiate objects from it?
The syntax for declaring a class in Kotlin is straightforward and follows this format:
class ClassName {
// Properties
// Member functions
// Constructors
// Other declarations
}
Class Name
The name of the class should follow Kotlin’s naming conventions, usually in PascalCase. It should be a meaningful identifier that reflects the purpose or nature of the objects to be created from the class.
Properties
Properties represent the state or characteristics of the objects. They define the data that each object will hold. Properties can be declared as val (read-only) or var (mutable).
What are Member Functions?
Member functions define the behavior or actions that objects can perform. These functions are associated with specific instances of the class and can access the properties of those instances.
What are Constructors?
Constructors are special functions that are responsible for initializing the newly created objects. In Kotlin, a primary constructor can be declared directly within the class declaration itself. Secondary constructors can also be defined to provide alternative ways of creating objects.
Other Declarations
Along with properties, member functions, and constructors, a class can include other declarations such as nested classes, interfaces, companion objects, and more.
Kotlin Class and Object Example
Open Kotlin Playground to Test the following program:
class Employee {
// Properties
var name: String = ""
private var id: Int = 0
private var age: Int? = null
// Member function
fun details()
{
println("Hello Welcome to Employee portal: $name")
println("Your Employee ID: $id")
println("Age : $age")
}
// Constructor
constructor(name:String, id:Int, age:Int)
{
this.name = name
this.id = id
this.age = age
}
}
fun main()
{
val emp = Employee("Nikky", 4545, 45)
emp.details()
}
Output
Hello Welcome to Employee portal: Nikky
Your Employee ID: 4545
Age : 45
Kotlin Classes Declaration
The Employee class is declared, which serves as a blueprint for creating employee objects.
Inside the class, there are properties, a member function, and a constructor.
Properties
- The class has three properties: name, id, and age.
- The name property is declared as a mutable variable of type String and is initially assigned an empty string.
- The id and age properties are declared as private mutable variables of type Int and Int? (nullable integer), respectively. They are initially assigned values of 0 and null.
Member Function
The class has a member function named details(). The details() function prints the employee details, including the name, ID, and age.
Constructor
The class has a constructor that takes three parameters: name, id, and age. The constructor initializes the corresponding properties of the Employee object with the provided parameter values using the this keyword.
Main Function
The main() function serves as the entry point of the program.
An instance of the Employee class is created using the Employee constructor with the values “Nikky”, 4545, and 45.
The details() function is called on the emp object to print the employee details.
How to avoid secondary constructor in Kotlin Classes ?
Let’s convert the secondary constructor to a primary one. We can make slight changes to the above program by getting rid of the secondary constructor (‘constructor(name: String, id: Int, age: Int)’).
class Employee (var name: String, private var id: Int, age: Int)
{
}
After this change
class Employee(
var name: String, private var id: Int, age: Int
) {
private var age: Int? = age
// Member function
fun details()
{
println("Hello Welcome to Employee portal: $name")
println("Your Employee ID: $id")
println("Age : $age")
}
}
fun main()
{
val emp = Employee("Nikky", 4545, 45)
emp.details()
}
How to make a simple calculator in Kotlin using Class and Object?
Define the Calculator class
class Calculator {
// Member functions for different operations
fun add(a: Int, b: Int): Int {
return a + b
}
fun subtract(a: Int, b: Int): Int {
return a - b
}
fun multiply(a: Int, b: Int): Int {
return a * b
}
fun divide(a: Int, b: Int): Double {
return a.toDouble() / b.toDouble()
}
}
Implement the main function
fun main()
{
// Create an instance of the Calculator class
val calculator = Calculator()
// Perform calculations
val resultAdd = calculator.add(10, 5)
val resultSubtract = calculator.subtract(10, 5)
val resultMultiply = calculator.multiply(10, 5)
val resultDivide = calculator.divide(10, 5)
// Print the results
println("Addition: $resultAdd")
println("Subtraction: $resultSubtract")
println("Multiplication: $resultMultiply")
println("Division: $resultDivide")
}
Output
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Kotlin classes form the backbone of object-oriented programming in the language, providing a powerful toolset for creating and managing objects.