CHAPTER 13
Beginner
Classes and Objects
Updated: May 18, 2026
5 min read
# CHAPTER 13
Classes and Objects
1. Chapter Introduction
Up to this point, we have used standard data types likeInt and String. But what if you are building an HR application? You need a User type or an Employee type. This is where Object-Oriented Programming (OOP) begins. In this chapter, we will learn how to create blueprints (Classes) and bring them to life as actual entities in memory (Objects).
2. Learning Objectives
By the end of this chapter, you will be able to:-
Define a
classin Kotlin.
- Instantiate an Object from a class.
- Add Properties (state) to a class.
- Add Methods (behavior) to a class.
-
Understand visibility modifiers (
public,private).
3. What is a Class?
A Class is a blueprint or template. It defines what data an entity holds, and what actions it can perform. For example, aCar blueprint says every car has a color and can drive().
kotlin
4. What is an Object?
An Object is an actual, physical instance of that blueprint created in the computer's memory. You can create multiple objects from a single class.In Java, you create objects using the new keyword (e.g., Car myCar = new Car()).
Kotlin completely removed the new keyword! You just call the class name like a function.
kotlin
5. Properties vs. Java Fields
In Java, you have to write hidden private fields and expose them using bulky Getter and Setter methods (getColor(), setColor()).
In Kotlin, you simply declare properties. Kotlin automatically generates the Getters and Setters for you under the hood!
kotlin
6. Visibility Modifiers
Sometimes you want to hide data inside a class so other parts of the code cannot accidentally break it. This is called Encapsulation.-
public: (Default in Kotlin). Accessible from anywhere.
-
private: Accessible ONLY from inside the class itself.
-
protected: Accessible inside the class and its subclasses (covered in Chapter 15).
-
internal: Accessible only within the same module (e.g., the same App or Library).
kotlin
7. Mini Project: Employee Management System
Let's build a simple system to model an Employee.
kotlin
8. Common Mistakes
-
Using the
newkeyword: Java developers constantly typeval c = new Car(). The Kotlin compiler will throw a syntax error. Just useCar().
-
Forgetting parentheses: Typing
val myCar = Carinstead ofval myCar = Car(). The first assigns the class reference, the second actually creates the object instance in memory.
9. Best Practices
-
Hide State: Make properties
privateif they should not be modified directly from outside the class. Force users to use methods (likedeposit()) to interact with sensitive data safely.
10. Exercises
-
1.
Create a class
Bookwith propertiestitleandauthor.
-
2.
Add a method
readBook()that prints "Reading [title] by [author]".
-
3.
In
main(), create an object, set the properties, and call the method.
11. MCQs with Answers
Question 1
What is a Class in OOP?
Question 2
What is an Object in OOP?
Question 3
What keyword is required to instantiate an object in Kotlin?
Question 4
What does Kotlin automatically generate when you declare a property like var name = "Bob"?
Question 5
What is the default visibility modifier for classes and properties in Kotlin?
Question 6
If you mark a property as private, who can access it?
Question 7
What OOP principle refers to hiding internal data and requiring users to interact with it via public methods?
Question 8
How do you call a method drive() on an object named myCar?
Question 9
Which modifier allows access only within the same module (e.g., the same compiled project)?
12. Interview Questions
-
Q: Explain why Kotlin eliminated the
newkeyword. (Answer: To make the language more concise and readable. Calling a constructor looks exactly like calling a regular function).
- Q: How does Kotlin handle Getters and Setters differently from Java?
13. Summary
Classes and Objects form the core of modern software architecture. A class defines the structure (properties) and the capabilities (methods). Kotlin massively improves upon Java's OOP syntax by removing thenew keyword and automatically generating Getters and Setters, eliminating hundreds of lines of boilerplate code while maintaining strict Encapsulation via visibility modifiers.
14. Next Chapter Recommendation
OurCar class hardcoded the color to "Red". If we want a blue car, we have to change it *after* creating it. This is inefficient. In Chapter 14: Constructors and Initialization, we will learn how to pass data into an object at the exact moment it is created.