Lesson 1
Classes and Objects in Kotlin
Classes and Objects

Let's dive into a foundational concept in Object-Oriented Programming (OOP): Classes and Objects. If you have already explored OOP concepts in other programming languages or previous units, this might serve as a good reminder. If not, no worries; we'll start from the basics.

Classes and objects are the building blocks of OOP. A class acts as a blueprint for creating objects, which are instances of the class. Understanding these basics is essential before moving on to more advanced OOP topics like inheritance, polymorphism, and encapsulation.

Defining a Class

In Kotlin, a class is defined using the class keyword. Properties are directly defined in the primary constructor, making the code more concise. Here's a simple example:

Kotlin
1// Defining a class named Person 2class Person(val name: String, var age: Int) // 'name' is read-only, 'age' is mutable
  • val creates a read-only property (immutable).
  • var creates a mutable property that can be updated.

A class can also have methods (functions) and initialization logic. Here's an example with an initialization block:

Kotlin
1class Person(val name: String, var age: Int) { 2 init { 3 println("A person named $name, aged $age, has been created.") 4 } 5}
What is an Object?

An object is an instance of a class. It represents a specific example of the class and holds the characteristics that define the class.

Objects have three main characteristics:

  • State: The data or attributes of the object. In the Person class, the name and age represent the object's state.
  • Behavior: The methods and functions that the object can perform.
  • Identity: A unique identifier that distinguishes the object from others, even if they have the same state.

To understand object identity in Kotlin, consider this example:

Kotlin
1fun main() { 2 val person1 = Person("Alice", 25) 3 val person2 = Person("Alice", 25) 4 val person3 = person1 5 6 println(person1 == person2) // true (compares values) 7 println(person1 === person2) // false (compares references) 8 println(person1 === person3) // true (same object reference) 9}
Using Constructors

Kotlin integrates the constructor directly into the class declaration as a primary constructor. You can initialize the object's properties using this constructor:

Kotlin
1class Person(val name: String, val age: Int)

For more complex initialization, use the init block:

Kotlin
1class Person(val name: String, val age: Int) { 2 init { 3 require(age > 0) { "Age must be positive" } 4 } 5}
Overloading Constructors

In Kotlin, constructor overloading is typically achieved using default parameter values in the primary constructor:

Kotlin
1class Car(val brand: String = "Unknown", val year: Int = 2000)

This approach removes the need for multiple constructors in most cases. However, you can define secondary constructors for advanced scenarios:

Kotlin
1class Car(val brand: String, val year: Int) { 2 3 // Secondary constructor 4 constructor() : this("Unknown", 2000) { 5 println("Secondary constructor used.") 6 } 7}
Member Functions

Member functions define the behavior of the object using the fun keyword. For example, you can define a method to display an object's data:

Kotlin
1class Person(private val name: String, private val age: Int) { 2 3 fun display() { 4 println("Hello! My name is $name, and I am $age years old.") 5 } 6}
Creating Objects from Classes

Once you have defined a class, you can create objects (instances of the class) by calling the constructor directly (no new keyword is needed). Here's how to create and use objects:

Kotlin
1class Person(private val name: String, private val age: Int) { 2 3 fun display() { 4 println("Name: $name, Age: $age") 5 } 6} 7 8fun main() { 9 val person = Person("Alice", 30) // Creating an object using the constructor 10 person.display() // Displaying the object's data 11}

Here, we create an object person with the name "Alice" and age 30, and then call its display method to show its data.

Why It Matters

Understanding classes and objects is critical because they enable you to model real-world entities in your programs. For instance, a Person class helps you create multiple person objects with different names and ages, enabling you to manage and manipulate data efficiently. This principle is the backbone of complex software systems, such as modeling users in an app or entities in a database.

With this knowledge, you will be better prepared to approach more advanced OOP techniques and design patterns, allowing you to write clean, modular, and scalable code. Let's get started with the practice section to gain more hands-on experience.

Ready to code? Let's dive in!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.