In Kotlin, a class is a blueprint for creating objects. Each object created from a class can have its own properties (variables) and methods (functions). Let's dive into how to define and use classes and objects in Kotlin!
To define a class, use the class
keyword followed by the class name. You can define properties and methods inside the class.
// Defining a class class Car(val make: String, val model: String) { fun drive() { println("Driving the $make $model") } }
This Car class has two properties: make
and model
, and a method called drive
that prints a message when invoked.
An object is created from a class using the new
keyword in other languages, but in Kotlin, we simply instantiate a class directly.
// Creating an object val myCar = Car("Toyota", "Camry") myCar.drive() // Output: Driving the Toyota Camry
Here, myCar is an object of the class Car. It holds the make and model of the car, and we can call its drive
method.
You can also initialize properties directly in the class constructor. In Kotlin, properties are defined inside the class constructor and automatically initialized with values.
// Class with properties class Person(val name: String, val age: Int) { fun greet() { println("Hello, my name is $name and I am $age years old.") } } // Creating an object val person1 = Person("Alice", 25) person1.greet() // Output: Hello, my name is Alice and I am 25 years old.
In this example, the Person class has a constructor with two parameters: name
and age
. The object person1 is created with these values, and we can call the greet
method to display the information.
You can also define default values for the class properties in the constructor. This allows you to create objects without passing all the values explicitly.
// Class with default values class Car(val make: String = "Toyota", val model: String = "Camry") { fun drive() { println("Driving the $make $model") } } // Creating an object with default values val defaultCar = Car() defaultCar.drive() // Output: Driving the Toyota Camry
Here, if no values are passed, the object defaultCar will use the default values ("Toyota" and "Camry").
Kotlin allows you to define a primary constructor and secondary constructors. The primary constructor is declared directly in the class header, while secondary constructors are declared inside the class body.
// Class with secondary constructor class Person { var name: String var age: Int constructor(name: String, age: Int) { this.name = name this.age = age } fun introduce() { println("Hello, my name is $name and I am $age years old.") } } // Creating an object using the secondary constructor val person2 = Person("Bob", 30) person2.introduce() // Output: Hello, my name is Bob and I am 30 years old.
The Person class uses a secondary constructor to initialize the name
and age
properties. The object person2 is created with values passed to the secondary constructor.
Kotlin has a special class type called data classes that automatically provides methods like toString()
, hashCode()
, and equals()
for you. This is useful when you only need to store data in an object.
// Defining a data class data class Car(val make: String, val model: String) // Creating an object val car1 = Car("Honda", "Civic") println(car1) // Output: Car(make=Honda, model=Civic)
With the data class, Kotlin automatically generates methods for comparing objects and converting them to a string format.
Here are some challenges for you to practice:
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!