Kotlin Classes & Objects

Kotlin classes and objects are the foundation of Object-Oriented Programming (OOP).
Kotlin improves on Java by providing concise syntax, safety, and powerful features.
1. Class Basics
Simple Class
Creating an Object
2. Primary Constructor (Most Important)
val→ read-only propertyvar→ mutable property
Usage:
3. Init Block
Executed when the object is created.
4. Secondary Constructor
Used for alternative initialization.
5. Properties & Custom Getters/Setters
field → backing field
6. Visibility Modifiers
| Modifier | Scope |
|---|---|
public | Everywhere (default) |
private | Same file/class |
protected | Class & subclasses |
internal | Same module |
7. Methods in Classes
8. this Keyword
Refers to the current object.
9. Inheritance Basics
10. Abstract Classes
11. Interfaces
12. Data Classes (Deep)
Features:
copy()componentN()(destructuring)Auto
equals()&hashCode()
13. Object Declaration (Singleton)
14. Companion Object (Static-like)
15. Anonymous Objects
16. Sealed Classes (Brief)
17. Real-World Example
Summary
✔ Concise constructors
✔ Properties + methods together
✔ Less boilerplate than Java
✔ Strong OOP + functional mix
