Kotlin – Classes and Objects: Core of OOP in Kotlin
Introduction – Why Learn Kotlin Classes and Objects?
Classes and objects form the foundation of object-oriented programming in Kotlin. A class defines the blueprint, while an object represents a specific instance of that class. Kotlin enhances class design with features like primary constructors, initializers, default parameters, and concise property declarations—making object creation cleaner and more expressive than in Java.
In this guide, you’ll learn:
- How to declare classes and create objects in Kotlin
- Use primary and secondary constructors
- Define properties and member functions
- Real-world examples of encapsulating logic into classes
What Is a Class in Kotlin?
A class is a user-defined type that bundles properties (data) and functions (behavior).
Basic Class Declaration:
class Person {
var name: String = ""
var age: Int = 0
fun greet() {
println("Hello, my name is $name and I'm $age years old.")
}
}
Creating and Using an Object
val person = Person()
person.name = "Alice"
person.age = 25
person.greet()
Output:
Hello, my name is Alice and I'm 25 years old.
✔️ Objects are created using the class name followed by parentheses.
Kotlin Primary Constructor
Kotlin supports inline constructor declaration in the class header.
class Student(val name: String, var grade: Int) {
fun showInfo() {
println("$name is in grade $grade")
}
}
val student = Student("Bob", 10)
student.showInfo()
Output:
Bob is in grade 10
✔️ val/var in the constructor auto-defines class properties.
Secondary Constructor (Optional)
Use when additional initialization paths are needed.
class Book {
var title: String
var author: String
constructor(title: String, author: String) {
this.title = title
this.author = author
}
}
val book = Book("1984", "George Orwell")
Initializer Block – init
Runs automatically during object creation, ideal for validations.
class User(val name: String) {
init {
println("User $name created")
}
}
Access Modifiers
| Modifier | Scope |
|---|---|
public | (default) Visible everywhere |
private | Visible only inside the class |
protected | Visible in class and subclasses |
internal | Visible within the same module |
Member Functions and Properties
class Rectangle(val width: Int, val height: Int) {
fun area(): Int = width * height
}
val r = Rectangle(5, 3)
println(r.area()) // 15
✔️ Functions inside a class are called member functions.
Class with Default Parameter Values
class Vehicle(val brand: String = "Tesla", val speed: Int = 120)
val car = Vehicle()
println("${car.brand} goes at ${car.speed} km/h")
Output:
Tesla goes at 120 km/h
Common Mistakes
| Mistake | Fix |
|---|---|
Forgetting to use val or var | Required to declare properties in constructors |
| Overusing secondary constructors | Prefer primary with default values or init blocks |
Ignoring init block logic | Use it for validation and logging during creation |
| Not encapsulating logic properly | Use methods to interact with internal data |
Best Practices for Kotlin Classes and Objects
| Practice | Why It Helps |
|---|---|
Use val for immutable properties | Improves safety and clarity |
| Keep classes small and focused | Encourages modular and testable design |
| Use primary constructor if possible | Cleaner and more idiomatic Kotlin |
Use access modifiers (private, etc.) | Protect class internals and expose only what’s needed |
Summary – Recap & Next Steps
Classes and objects in Kotlin enable clean, structured programming using encapsulation, modularity, and reusability. Kotlin simplifies class creation with primary constructors, concise syntax, and safe access control.
Key Takeaways:
- Classes define structure; objects are instances with real data
- Use primary constructors for cleaner property setup
- Use
initfor setup logic - Prefer
valunless mutation is necessary
Practical Use:
Classes and objects are essential in Android UI components, domain models, data wrappers, API responses, and logic encapsulation in Kotlin applications.
FAQs – Kotlin Classes and Objects
What’s the difference between a class and an object in Kotlin?
A class defines a blueprint, while an object is an instance created from that blueprint.
Is it mandatory to use constructors in Kotlin?
No. Kotlin provides a default constructor if none is specified. You can also define primary and secondary constructors.
Can Kotlin classes have methods?
Yes. You can define functions inside classes, which are called member functions.
How do you initialize properties in Kotlin classes?
Use val or var in the primary constructor or initialize them inside the class body.
Can Kotlin support multiple constructors?
Yes. Use secondary constructors alongside the primary constructor if needed.
Share Now :
