Kotin supports classes based on object-oriented programming language concepts. We all have learned about OOP concepts and Kotlin supports all of them. There are 7 concepts of OOPS Abstraction, Encapsulation, Polymorphism, Inheritance, association, aggregation, and composition. In classes, we can use real-world naming conventions. In this tutorial, we will learn how to define classes in Kotlin, and how to create objects of class and use their functions.
Mastering Kotlin Classes: A Step-by-Step Guide for Beginners
What is a class in Kotlin?
A class is like a blueprint of a well-named data structure. The class encapsulates properties and methods which determine the behavior of the class. In another class, we can create an object of the class that represents a Blueprint and use its functions and variables. This will enable functionality transfer and lead to code redundancy.
How to Define a Class in Kotlin:
We can define a class in Kotlin easily. All we have to do is use the class keyword.
1 2 3 4 5 |
class CustomerDetails { // Class Scope } |
Class with default properties in Kotlin:
In a class, we can define default values to a property so in any case values were not assigned then it will assign the default values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class CustomerDetails { // Properties var customerName: String = "Not Added Yet" var customerAge: Int = 0 var customerAddress: String = "Not Added Yet" // Method fun printCustomerDetails() { println("Customer Name: $customerName") println("Customer Age: $customerAge") println("Customer Address: $customerAddress") } } |
Creating objects and calling class functions:
To create an object of a class we have to follow a syntax var + variable name = class Name. This will create a class instance and we can use class variables and functions.
1 2 3 4 5 6 7 |
fun main() { val customerDetailsObject = CustomerDetails() customerDetailsObject.printCustomerDetails() } |
Output:
1 2 3 4 5 |
Customer Name: Not Added Yet Customer Age: 0 Customer Address: Not Added Yet |
Assigning values to class variables(Properties):
We can assign a class property(variable) a value using the object name the dot operator and the variable name.
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 CustomerDetails { // Properties var customerName: String = "Not Added Yet" var customerAge: Int = 0 var customerAddress: String = "Not Added Yet" // Method fun printCustomerDetails() { println("Customer Name: $customerName") println("Customer Age: $customerAge") println("Customer Address: $customerAddress") } } fun main() { val customerDetailsObject = CustomerDetails() customerDetailsObject.customerName = "KotlinGuide" customerDetailsObject.customerAge = 20 customerDetailsObject.customerAddress = "Internet" customerDetailsObject.printCustomerDetails() } // Output Customer Name: KotlinGuide Customer Age: 20 Customer Address: Internet |
Constructors in Kotlin class:
In Kotlin we can initialize objects directly when we create them using the constructor. When we define variables in the class’s header, then by default it operates as a Constructor. There is no need to define Getter and Setter in the class.
1 2 3 4 5 6 7 |
class User(val name: String, var age: Int) { fun printInfo() { println("Name: $name, Age: $age") } } |
Calling constructor in main class in Kotlin:
1 2 3 4 5 6 7 8 9 10 |
fun main() { val user = User("KotlinGuide", 28) user.printInfo() } // Output Name: KotlinGuide, Age: 28 |
How do you define multiple constructors in Kotlin class?
In Kotlin class we can define multiple constructors within a single class. This is called Constructor overloading. All the constructor names are the same but they perform different tasks.
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 |
class User { var userName: String var userAge: Int // Primary Constructor constructor(name: String, age: Int) { this.userName = name this.userAge = age } // Secondary Constructor constructor(name: String) { this.userName = name this.userAge = 22 // Default value } fun printUserInfo() { println("User Name: $userName, Age: $userAge") } } fun main() { val object1 = User("Sam", 24) val object2 = User("Gini") object1.printUserInfo() object2.printUserInfo() } // Output User Name: Sam, Age: 24 User Name: Gini, Age: 22 |
Data class in Kotlin:
Unlike normal class data class can be declared using the data keyword. The main purpose of data class is to hold data only and they do not provide additional functionality. In the data class functions like toString(), copy(), equals(), and hashCode() functions are inbuilt. In other words, when you only want a class to store data and do not want to implement functions in that class to perform tasks, That class is represented as a data class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
data class UserDetails(val name: String, val age: Int) fun main() { // Creating instances of the User data class val userObject1 = UserDetails("Tom", 28) val userObject2 = UserDetails("Mike", 21) // toString() - Print the object println(userObject1) // equals() - Check if two objects are equal println(userObject1 == userObject2) // copy() - Create a copy of the object val userObject3 = userObject1.copy(age = 30) println(userObject3) } // Output UserDetails(name=Tom, age=28) false UserDetails(name=Tom, age=30) |
Conclusion:
Classes are a fundamental concept of OOP(Object-oriented programming) and essential to pursue a career in Kotlin development. We have covered how to create a class, What a is class, and how you can perform various operations using a class in Kotlin. Keep practicing them, Happy coding.