Kotlin OOP

Kotlin Object-Oriented Programming (OOP)

Kotlin is a multi-paradigm programming language, meaning it supports functional programming, but its backbone is heavily rooted in Object-Oriented Programming (OOP).

If you want to build robust, scalable, and modern applications (like Android apps), mastering OOP is absolutely essential.


1. What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that perform operations on the data. Object-Oriented Programming is about creating objects that contain both data and functions together.

Object-oriented programming offers several major advantages over procedural programming:


2. Classes and Objects: The Core Concept

The two most important aspects of object-oriented programming are classes and objects.

To understand the difference, let's look at a real-world analogy:

Example 1: A Car

Example 2: Fruit

A class is a template for objects, and an object is an instance of a class. When individual objects are created, they inherit all the variables and functions from the class, but they can hold their own unique data!


3. The Four Pillars of OOP

As you advance in your Kotlin journey, you will constantly use the "Four Pillars" of Object-Oriented Programming. Here is a brief overview of what they mean:

1. Encapsulation

Encapsulation is the concept of wrapping data (variables) and code acting on the data (methods) together as a single unit. In Kotlin, this means keeping your properties private or restricted and only allowing them to be modified via specific functions to prevent accidental interference.

2. Inheritance

Inheritance allows one class to inherit the properties and functions of another class. This heavily promotes code reusability. For example, a Dog class can inherit from an Animal class. We will cover this in detail in a later lesson.

3. Polymorphism

Polymorphism means "many forms". It allows us to perform a single action in different ways. For example, an Animal class might have a makeSound() function. The Dog class overrides it to bark, while the Cat class overrides it to meow.

4. Abstraction

Abstraction is about hiding complex implementation details and showing only the essential features of an object. In Kotlin, this is achieved using abstract classes or interfaces.


4. Everything is an Object in Kotlin

You might not have realized it, but if you have been writing basic Kotlin code, you have already been using classes and objects!

In Kotlin, everything is an object. Even basic data types like numbers, strings, and booleans are objects under the hood.

Kotlin Data Types as Objects

fun main() {
  val myText = "Hello World" 
  // myText is an Object of the String class!
  

// Because it's an object, it has access to built-in class functions: println(myText.uppercase()) println(myText.length) }

In the example above, myText is an instance (object) of the String class. The String class provides functions like .uppercase() and properties like .length that the object can use.

In the next tutorials, we will learn how to build our own custom classes!


Exercise 1 of 2

?

What does OOP stand for?

Exercise 2 of 2

?

If "House" is a class, which of the following represents an object of that class?