Updated March 30, 2023
Introduction to Kotlin class
The kotlin class is one of the keywords, and it is declared using the class header specifying its parameter type. It created the primary constructor around the class body separated by using the curly braces both the header and the body are optional. If the class has no body, then the curly braces are omitted on the script; the kotlin class have both primary and secondary constructors; the primary constructor is the class header part. It directly goes to the class name and optional type parameters annotated with the primary constructor’s visibility modifiers.
Syntax of Kotlin class
In kotlin language has many default keywords, variables and functions for to implement the application. Like that, class is the main role of the language, and it is created in a separate file similar to the java language.
class name(parameters)
{
---variable declarations and method declarations---
}
class name1()
{
--some codes similar to the above parent class name—
}
fun main()
{
name(values)
name1()
}
The above codes are the basic syntax for class declarations in the kotlin language. We can able to create more than one class in the file so that the object will be created in the main function.
How does the class work in Kotlin?
The classes in kotlin are declared using the keyword like class, and it consists of a name, it is a blueprint of the runtime entity, and the object is created using its state. The object state includes both behaviour and state; the declaration consists of a class header and the class body surrounded by the curly braces, which is similar to the java.
It allows to create the class objects and include its class members and functions; it can control the visibility of the class members variables using different keywords. So we will be able to create one class and its object through which we will access different data members of the class. Classes contain many types like nested class, inner class, and Anonymous Inner class has been created and default static keyword will be used hence it can be accessed without creating any object of that class type.
Like that similarly, Inner class is one of the types and it is marked as the Inner then it will be called as the Inner, and the class can be accessed by the data member of the outer class. Similarly, an anonymous inner class is a pretty good concept that makes writing the code easy compared to the other types of classes.
Examples of Kotlin class
Given below are the examples of Kotlin class:
Example #1
Code:
open class firstclas {
open fun clasmethd() {}
}
abstract class subClas : firstclas() {
abstract override fun clasmethd()
}
class firstExample(home: String) {
val homeNames = "Welcome To Our Company Please select your favourite company: $home".also(::println)
init {
println("Have a Nice day users ${home}")
}
val second = "Please try again: ${home.length}".also(::println)
init {
println("To print your home builder details ${home.length}")
}
class nestedclss {
fun mthd() = "Welcome To The RK Group of Constructions"
fun mthd1() = "Welcome To The KR Group of Constructions"
fun mthd2() = "Welcome To The SV Group of Constructions,BSCPL Infrastructure"
fun mthd3() = "Welcome To The VS Group of Constructions, Ozone Group Bangalore"
fun mthd4() = "Welcome To The VSR Group of Constructions, BSCPL Infrastructure"
fun mthd5() = "Welcome To The Royal Knit Group of Constructions, Shriram Properties"
fun mthd6() = "Welcome To The Mithun Constructions, Pacifica Companies"
fun mthd7() = "Welcome To The JK Group of Constructions, DRA Group"
fun mthd8() = "Welcome To The Vijay Shanthi Builders, Prestige Group,Olymia Group, Godrej Properties,Brigade Group"
fun mthd9() = "Welcome To The Kay Arr and Co Rio Grande, Tata value Homes, Brigade Group, Akshaya Homes"
fun mthd10() = "Welcome To The JB, Eternia, ITC Grand Chola, DEEJOS Architects, Bharath Engineering Construction company,Jehovah Nissi Archfirm"
}
}
fun main() {
firstExample("Welcome To My Domain its the first example that related to the kotlin class ")
val demo = firstExample.nestedclss().mthd()
print(demo)
val demo1 = firstExample.nestedclss().mthd1()
println(demo1)
val demo2 = firstExample.nestedclss().mthd7()
println(demo2)
val demo3 = firstExample.nestedclss().mthd2()
println(demo3)
val demo4 = firstExample.nestedclss().mthd3()
println(demo4)
val demo5 = firstExample.nestedclss().mthd4()
println(demo5)
val demo6 = firstExample.nestedclss().mthd5()
println(demo6)
val demo7 = firstExample.nestedclss().mthd6()
println(demo7)
val demo8 = firstExample.nestedclss().mthd8()
println(demo8)
val demo9 = firstExample.nestedclss().mthd9()
println(demo9)
val demo10 = firstExample.nestedclss().mthd10()
println(demo10)
}
Output:
In the above example, we used class with nested class type and called the main methods after creating the object.
Example #2
Code:
class secondExample {
init {
println("Your Second Example that related to the kotlin class")
}
constructor(x: Int) {
println("Your second example class have the constructor $x")
}
class subclas
{
fun add()
{
var c: Int = 12
var a : Int = 13
var b :Int = 15
c= a + b
print(c)
}
}
}
fun main() {
secondExample(1)
secondExample(2)
secondExample(3)
secondExample(4)
secondExample(5)
secondExample(6)
secondExample(7)
secondExample(8)
secondExample(9)
secondExample(10)
}
Output:
In the second example, we used to create declare class and use the constructor for to create the object in main.
Example #3
Code:
fun main(args: Array<String>) {
var tech1 :techDevice = object:techDevice {
override fun Gadgets() {
println("Welcome To My Domain its the third example that related to the kotlin class ")
println("Some of the Gadgets companies are Samsung,Onida, BPL, EAirtec, Airtel, Vu,Sony, Oscar, TCL, LG, Panasonic,Xiaomi")
}
}
tech1.Gadgets()
Technology()
var t= Technology()
t.techmeth(1245,"Sony",176457.32f)
println("${t.techName}")
}
interface techDevice {
fun Gadgets()
}
class Technology {
var techno: Int = 0
var techName: String = ""
var techamnt: Float = 0.toFloat()
fun techmeth(i: Int,str: String, flt: Float ) {
techno=i
techName=str
techamnt=flt
println("Your total amount is calculated and see the format ${techno} holder :${techName} techamnt :${techamnt}")
}
}
Output:
In the final example, we used to create class, interface, and some methods to create the object in main and call it the same.
Conclusion
In kotlin language, we used a lot of methods, variables, and other keywords like that class is the keyword and also which is similar to the java language. So by using the class, we can declare the methods, variables, and other keywords; also, we can use other default and customized methods in the kotlin codes.
Recommended Articles
This is a guide to Kotlin class. Here we discuss the introduction, syntax, and working of class in kotlin along with different examples for better understanding. You may also have a look at the following articles to learn more –