Updated April 4, 2023
Introduction to Kotlin Object
Kotlin object is one of the keywords that combines both declaring the class and also creating the instance of one user action to another action it may be of the nested class that can hold with the members related to the outer classes and these members can’t require with an instance of the outer type of classes also the kotlin object creates the anonymous type of class it referred as the anonymous objects and it is used to create an object of the slight modification of some type of classes and interface for without declaring the subclasses.
Syntax
In kotlin object is the main keyword that can be used to create the instance of the classes and expressions for the need to create the slight modification of some classes and interface for without declaring the subclass.
object objectName
{
var VarName: datatype
fun method()
{
VarName = values
}
}
fun main(args:Array<String>)
{
val vars: datatype
vars = objectName.method()
}
The above codes are the basic syntax used to create the object in the kotlin application it depends upon the requirement the classes and methods are used and called in the codes.
How does object work in Kotlin?
The kotlin provides an easy way to create the object declaration feature using the object keyword. Basically, an object declaration contains the properties methods and etc. According to kotlin object is a special class that contains only has one instance and if we create the class with the object keyword that can be used for instead of the class. And also the kotlin compiler it makes the constructor for creating the object it may be of any modifiers like public-private and even protected for creates the static reference of the object and it initializes the reference in the static block. Using a new operator and the keyword requires creating the instance of the class and also it requires the single and postfix argument for a call to the constructor. The name of the constructor will be provided using the name of the class that can be instantiated using the object creation. The instance mainly refers to the memory areas and its address of the class once the class is instantiated using a new keyword that becomes the object of the thing. So that the instantiation is also known as the construction and it’s not having all the classes can be instantiated so it is mainly referred as the abstract classes which are the classes that can be instantiated are called the concrete classes.
Example #1
Code:
class Example1
{
private var Active: Boolean = true
fun RobotMode() {
Active = true
}
fun RobotModeOff() {
Active = false
}
fun RobotOperations(electronic: String) {
if (Active == true)
println("$electronic electronic is on.")
else
println("$electronic electronic is off.")
}
class subClass {
fun month() = "Have a Nice Day users please try again"
}
private val strs: String = "Please see below list of months in the year"
inner class exam1 {
fun mnths() = strs
fun exm2()
{
println("January is the first month")
println("February is the second month")
println("March is the third month")
println("April is the fourth month")
println("May is the fifth month")
println("June is the sixth month")
println("July is the seventh month")
println("August is the eight month")
println("September is the ninth month")
println("October is the tenth month")
println("November is the eleventh month")
println("December is the twelth month")
}
}
}
fun main(args: Array<String>) {
println("Welcome To My Domain it’s the first example that related to the kotlin object")
val x = Example1()
val y = Example1()
x.RobotMode()
y.RobotModeOff()
x.RobotOperations("x")
y.RobotOperations("y")
val z = Example1.subClass().month()
print(z)
val demo1 = Example1().exam1().exm2()
print(demo1)
}
Output:
In the above example, we used to perform the Robotic and electronic device operations using object creation.
Example #2
Code:
fun main(args: Array<String>) {
var Electricals :Devices = object:Devices {
override fun TV() {
println("Welcome To My Domain its the second example that related to the kotlin object ")
println("Some of the tv companies are Samsung, Onida, BPL, EAirtec, Airtel, Vu,Sony, Oscar, TCL, LG, Panasonic, Xiaomi")
}
}
Electricals.TV()
Amounts()
var amt= Amounts()
amt.mthd1(426365,"Sivaraman",5653f)
println("${amt.name}")
}
interface Devices {
fun TV()
}
class Amounts {
var inv_no: Int = 0
var name: String = ""
var amnt: Float = 0.toFloat()
fun mthd1(inv: Int,n: String, am: Float ) {
inv_no=inv
name=n
amnt=am
println("Your total amount is calculated and see the format ${inv_no} holder :${name} amnt :${amnt}")
}
fun PC() {
println("Some of the PC manufacturer companies are: Sony, Dell, Lenovo, HCL, Infosys, Apple, Wipro, Microsoft, Hewlett-Packard")
}
fun Fridge() {
println("some of the fridge manufacturer companies are: Godrej, Whirlpool, Bosch, Samsung, Siemens, Electrolux, Videocon, LG, Haier, Panasonic ")
}
fun Laptop() {
println("some of the laptop manufacturer companies are Dell, Lenovo, HCL, HP, Wipro, Thinkpad, Apple, Samsung, Sony, Acer, NEC, Fujiitsu")
}
}
Output:
In the second example we calculate the electronic devices costs using classes and interface with methods. An object is created and called each method in the main method.
Example #3
Code:
class thirdExample {
private var strs: String = "<DependencyPath Path=Child:Sizes/>"
private var strs1: String = "<Node Parameter=Type Op=EQ Value=Sample/Access Path=Child:SampleSRLineItem Right=Read/Attribute Id=Supplier Op=EQ Path=Child:__Parent__ RValue=C193465/>OrderByAttribute Id=Node Name Path=/>"
private var strs2: String = "<Attribute Id=State Op=EQ Value=SupplierRequestState:Issued Path=Child:SampleSRLineItem/Child:__Parent__/>Attribute Id=State Op=EQ Value=SupplierRequestState:Complete Path=Child:SampleSRLineItem/Child:__Parent__/>"
var str3:String="See video attached Why would this happen?Checked the OOTB and options are not the same. "
fun demos() {
print("The first string input values are"+strs)
print("The second string input values are"+strs1)
print("The third string input values are"+strs2)
print("The fourth string input values are"+str3)
}
}fun main(args: Array<String>) {
println("Welcome To My Domain its the third example that related to the kotlin object")
val vars = thirdExample()
vars.demos()
}
Output:
The final example we used to create the objects for the method with some string datatype values.
Conclusion
In kotlin object is the most important one and it is predominant for performing the user operations in the application end. For each task the instance is created on the backend stack memory it will be allocated automatically and through its reference, the values will hold as separate variables.
Recommended Articles
This is a guide to Kotlin Object. Here we discuss the Introduction, syntax, and working of objects in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –