Introduction and Topic Actualization

Welcome to our next lesson! Today, we are focusing on Encapsulation, a crucial pillar of Object-Oriented Programming (OOP). We'll demystify the concept and master its implementation in Kotlin.

Encapsulation, much like a safe for valuables, ensures that our data in code is accessed and utilized appropriately.

Our exploration itinerary includes an overview of encapsulation, its implementation in Kotlin—specifically using private properties—and a hands-on tutorial on Accessors (getters and setters). Let's set forth!

A Closer Look at Encapsulation

Encapsulation wraps data (properties) and the methods manipulating that data into one coherent unit—a class in Kotlin. Central to Encapsulation is the confinement of data, which restricts outside access.

Consider your smartphone. Your interaction with it involves buttons; however, the underlying complexities are hidden. That is precisely what encapsulation accomplishes—exposing necessities while concealing complexities.

Encapsulation offers numerous advantages: it safeguards data from unwanted alteration, enhances usability by revealing only pertinent parts, and boosts modularity, thereby making your code more maintainable and adaptable.

Implementing Encapsulation in Kotlin

Encapsulation in Kotlin is achieved by controlling access to class properties, commonly by marking them as private. A private property or method can only be accessed within the class it is declared, safeguarding your class's data from unauthorized access and modification.

Consider the BankAccount class example to understand encapsulation in action:

In the BankAccount class, the balance property is private, meaning it cannot be directly accessed or modified from outside the class. This ensures that balance can only be modified through the deposit and withdraw methods, which include checks for valid transactions. Additionally, the printBalance method is private and demonstrates that not only properties but also methods can be encapsulated within a class, further controlling access and preventing unauthorized manipulation. The main method demonstrates creating an instance of BankAccount, making deposits and withdrawals, and shows that direct access to both balance and the printBalance method is not permitted, reinforcing the concept of encapsulation.

Understanding Accessors (Getters and Setters) in Kotlin

Kotlin automatically provides getters for all properties and setters for mutable properties (declared with var). Immutable properties (declared with val) only have a getter, since their values cannot be changed after initialization. To add specific behavior when a property is accessed or modified, such as logging, you can define custom getters and setters. The field identifier is used within these custom accessors to directly access the property's underlying value.

Consider the User class that includes a custom getter and setter for the age property, illustrating the use of field:

In this example, age is a mutable property with both a custom getter and setter, allowing additional actions (like printing messages) when the property is accessed or changed. The name property shows how to limit modifications by making the setter private, demonstrating control over property visibility and mutability in Kotlin.

Wrap up and Lesson Recap

Congratulations! You've successfully navigated through the nuances of encapsulation and understood the role of private properties and accessors in Kotlin.

Draw parallels to the real world—just like how the internal workings of a smartphone are hidden, encapsulation safeguards data within classes.

Next, brace yourself for quick-fire exercises to reinforce your learning. Well done, and continue your coding journey!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal