Introduction
Classes and objects are fundamental concepts in object-oriented programming (OOP). A class defines a blueprint for an object, and an object is an instance of a class. This chapter will cover the basics of creating classes and objects in Kotlin, along with properties, methods, constructors, and initializer blocks.
Defining a Class
A class in Kotlin is defined using the class
keyword followed by the class name. A class can contain properties (variables) and methods (functions).
Syntax
class ClassName {
// Properties
// Methods
}
Example
fun main() {
val person = Person("John", 25)
person.displayInfo()
}
class Person(val name: String, var age: Int) {
fun displayInfo() {
println("Name: $name, Age: $age")
}
}
Explanation:
class Person(val name: String, var age: Int) { ... }
: Defines a classPerson
with two propertiesname
(immutable) andage
(mutable).fun displayInfo() { ... }
: Defines a methoddisplayInfo
that prints the person’s information.val person = Person("John", 25)
: Creates an instance of thePerson
class.
Output:
Name: John, Age: 25
Creating Objects
An object is an instance of a class. You create an object by calling the class constructor.
Syntax
val objectName = ClassName(arguments)
Example
fun main() {
val person = Person("John", 25)
person.displayInfo()
}
class Person(val name: String, var age: Int) {
fun displayInfo() {
println("Name: $name, Age: $age")
}
}
Explanation:
val person = Person("John", 25)
: Creates an instance of thePerson
class by calling its constructor with arguments "John" and 25.person.displayInfo()
: Calls thedisplayInfo
method on theperson
object.
Output:
Name: John, Age: 25
Constructors
Primary Constructor
The primary constructor is part of the class header and initializes the class properties.
Syntax
class ClassName(val property1: Type, var property2: Type) {
// Properties
// Methods
}
Example
class Person(val name: String, var age: Int)
Secondary Constructor
A class can have one or more secondary constructors.
Syntax
class ClassName {
constructor(parameter1: Type, parameter2: Type) {
// Initialization code
}
}
Example
class Person {
val name: String
var age: Int
constructor(name: String, age: Int) {
this.name = name
this.age = age
}
}
Explanation:
constructor(name: String, age: Int) { ... }
: Defines a secondary constructor that initializes thename
andage
properties.
Initializer Blocks
Initializer blocks are used to initialize properties and execute code when an instance of the class is created.
Syntax
class ClassName(val property1: Type, var property2: Type) {
init {
// Initialization code
}
}
Example
fun main() {
val person = Person("Alice", 30)
person.displayInfo()
}
class Person(val name: String, var age: Int) {
init {
println("Person object is created with name: $name and age: $age")
}
fun displayInfo() {
println("Name: $name, Age: $age")
}
}
Output:
Person object is created with name: Alice and age: 30
Name: Alice, Age: 30
Properties and Methods
Properties
Properties are variables that belong to a class. They can be mutable (var
) or immutable (val
).
Example
class Car(val brand: String, var year: Int)
Methods
Methods are functions that belong to a class and define the behavior of objects.
Example
class Car(val brand: String, var year: Int) {
fun displayDetails() {
println("Brand: $brand, Year: $year")
}
}
Full Example
fun main() {
val car = Car("Toyota", 2021)
car.displayDetails()
}
class Car(val brand: String, var year: Int) {
fun displayDetails() {
println("Brand: $brand, Year: $year")
}
}
Output:
Brand: Toyota, Year: 2021
Visibility Modifiers
Kotlin provides visibility modifiers to control the visibility of classes, objects, interfaces, constructors, functions, properties, and their setters.
Example
public
: Visible everywhere (default).private
: Visible only within the file or class.protected
: Visible within the class and its subclasses.internal
: Visible within the same module.
class Example {
public val publicProperty = "Public"
private val privateProperty = "Private"
protected val protectedProperty = "Protected"
internal val internalProperty = "Internal"
fun showProperties() {
println(publicProperty)
println(privateProperty)
println(protectedProperty)
println(internalProperty)
}
}
Example Program with Classes and Objects
Here is an example program that demonstrates various aspects of classes and objects in Kotlin:
fun main() {
// Creating objects
val person = Person("John", 25)
person.displayInfo()
// Using secondary constructor
val student = Student("Alice", 20, "Computer Science")
student.displayInfo()
// Using initializer block
val car = Car("Toyota", 2021)
car.displayDetails()
// Visibility modifiers
val example = Example()
example.showProperties()
}
// Basic class with primary constructor
class Person(val name: String, var age: Int) {
fun displayInfo() {
println("Name: $name, Age: $age")
}
}
// Class with secondary constructor
class Student {
val name: String
var age: Int
val major: String
constructor(name: String, age: Int, major: String) {
this.name = name
this.age = age
this.major = major
}
fun displayInfo() {
println("Name: $name, Age: $age, Major: $major")
}
}
// Class with initializer block
class Car(val brand: String, var year: Int) {
init {
println("Car object is created with brand: $brand and year: $year")
}
fun displayDetails() {
println("Brand: $brand, Year: $year")
}
}
// Class with visibility modifiers
class Example {
public val publicProperty = "Public"
private val privateProperty = "Private"
protected val protectedProperty = "Protected"
internal val internalProperty = "Internal"
fun showProperties() {
println(publicProperty)
println(privateProperty)
println(protectedProperty)
println(internalProperty)
}
}
Output:
Name: John, Age: 25
Name: Alice, Age: 20, Major: Computer Science
Car object is created with brand: Toyota and year: 2021
Brand: Toyota, Year: 2021
Public
Private
Protected
Internal
Conclusion
In this chapter, you learned about classes and objects in Kotlin, including defining classes, creating objects, using primary and secondary constructors, initializer blocks, properties, methods, and visibility modifiers. Understanding these concepts is essential for writing modular and reusable code in your Kotlin programs.