Dec 19, 2022
Updated: Feb 15, 2023
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Kotlin is associated with classes and objects, along with its properties and functions.
To create a class, use the class keyword, and specify the name of the class:
class Car
{
var brand = ""
var model = ""
var year = 0
}
Let us know how to Create an Object
Now we can use the class named Car to create objects.
class Car {
var brand = ""
var model = ""
var year = 0
}
fun main() {
val c1 = Car()
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969
println(c1.brand)
println(c1.model)
println(c1.year)
}
Output:
Ford
Mustang
1969
Let us know how to Create multiple Object
class Car {
var brand = ""
var model = ""
var year = 0
}
fun main() {
val c1 = Car()
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969
val c2 = Car()
c2.brand = "BMW"
c2.model = "X5"
c2.year = 1999
println(c1.brand)
println(c2.brand)
}
output:
Ford
BMW
Constructors are basically used to initialize each and every object
class Car
{
var brand = ""
var model = ""
var year = 0
}
fun main()
{
val c1 = Car()
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969
}
Hope you understand the oops and syntax of kotlin in the next blog we are going to learn inheritence in kotlin.
Thank you
The journey of solving bug and completing project on time in Kotlin can be challenging and lonely. If you need help regarding other sides to Kotlin, we’re here for you!
Drop an email to us at contact@codersarts.com with the Project title, deadline, and requirement files. Our email team will revert back promptly to get started on the work.