

Kotlin OOP Class and Objects
Class and object are basic building blocks in object-oriented programming languages. A class is written by a programmer in a defined structure to create an object (computer science) in an object oriented programming language. It defines a set of properties and methods that are common to all objects of one type.For example, a class could be a car, which could have a color field, make field and model field. Another related class could be a truck, which would have similar fields, but not be exactly the same as a car. Both a car and a truck could be a kind of a third class which could be called a vehicle class. In this way, the programmer could create the parts of the program that are the same for both the car and the truck in programming the vehicle class, yet the programmer would be free to program how a car is different from a truck without duplicating all of the programming.
Kotlin class syntax
class ClassName {
// property
// member function
... .. ...
}
Example Kotlin Class
class Car {
var color: String = "Silver" //Default value for colors cars is Silver
var make: String = ""
var model: String = ""
get() = field
set(value) {
field = value
}
}
class Truck{
var color: String = "Silver" //Default value for colors trucks is Silver
var make: String = ""
var model: String = ""
get() = field
set(value) {
field = value
}
}
Objects from class in Kotlin
When you instantiate object of theCar
class and initialize the color
, make
and model
properties, it is passed to the setters parameter value
and sets field
to value
.Example of creating an object from class in Kotlin
fun main() {
val newCar = Car()
newCar.color = "Red"
newCar.make = "BMW"
newCar.model = "X5"
println("Car color is: " + newCar.color)
println("Car make is: " + newCar.make)
println("Car model is: " + newCar.model)
}
Another example of creating an object from class in Kotlin
fun main() {
val newTruck = Truck()
newTruck.color = "Blue"
newTruck.make = "Chevy"
newTruck.model = "Silverado"
println("Truck color is: " + newTruck.color)
println("Truck make is: " + newTruck.make)
println("Truck model is: " + newTruck.model)
}
Complete Example Code
class Car {
var color: String = "Silver" //Default value for colors cars is Silver
var make: String = ""
var model: String = ""
get() = field
set(value) {
field = value
}
}
class Truck{
var color: String = "Silver" //Default value for colors trucks is Silver
var make: String = ""
var model: String = ""
get() = field
set(value) {
field = value
}
}
fun main() {
val newCar = Car()
newCar.color = "Red"
newCar.make = "BMW"
newCar.model = "X5"
println("Car color is: " + newCar.color)
println("Car make is: " + newCar.make)
println("Car model is: " + newCar.model)
val newTruck = Truck()
newTruck.color = "Red"
newTruck.make = "BMW"
newTruck.model = "X5"
println("Truck color is: " + newTruck.color)
println("Truck make is: " + newTruck.make)
println("Truck model is: " + newTruck.model)
}
Output
Car color is: Red
Car make is: BMW
Car model is: X5
Truck color is: Blue
Truck make is: Chevy
Truck model is: Silverado
Car make is: BMW
Car model is: X5
Truck color is: Blue
Truck make is: Chevy
Truck model is: Silverado
Conclusion
In this page (written and validated by A. Gawali) you learned about Kotlin OOP Class and Objects . What's Next? If you are interested in completing Kotlin tutorial, your next topic will be learning about: Kotlin Class Constructor.
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.
Share On: |
Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.