KOTLIN Tutorial



OOP IN KOTLIN


Object-Oriented Programming (OOP) in Kotlin

Object-Oriented Programming (OOP) is a paradigm that organizes code into "objects" that combine data and methods. Kotlin fully supports OOP principles like classes, inheritance, and polymorphism.


1. Classes and Objects

In Kotlin, a class is a blueprint for creating objects. You can define properties and methods inside a class, and then instantiate objects from that class.

// Define a class
class Car(val make: String, val model: String) {
    fun drive() {
        println("Driving the $make $model")
    }
}

// Create an object from the class
val myCar = Car("Toyota", "Camry")
myCar.drive()  // Output: Driving the Toyota Camry
  

2. Constructors

Constructors are used to initialize the objectโ€™s properties when it's created. In Kotlin, you can define a primary constructor directly in the class header.

// Class with a primary constructor
class Person(val name: String, val age: Int) {
    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

val person1 = Person("Alice", 25)
person1.greet()  // Output: Hello, my name is Alice and I am 25 years old.
  

3. Inheritance

Inheritance allows one class to inherit properties and methods from another class. In Kotlin, inheritance is implemented using the open keyword.

// Base class
open class Animal(val name: String) {
    fun speak() {
        println("$name makes a sound.")
    }
}

// Derived class inherits from Animal
class Dog(name: String) : Animal(name) {
    fun bark() {
        println("$name is barking!")
    }
}

val dog = Dog("Buddy")
dog.speak()  // Output: Buddy makes a sound.
dog.bark()   // Output: Buddy is barking!
  

4. Polymorphism

Polymorphism allows methods to behave differently based on the object calling them. In Kotlin, polymorphism is achieved through method overriding.

// Base class with open function
open class Shape {
    open fun draw() {
        println("Drawing a shape")
    }
}

// Derived class overrides the draw function
class Circle : Shape() {
    override fun draw() {
        println("Drawing a circle")
    }
}

// Derived class overrides the draw function
class Square : Shape() {
    override fun draw() {
        println("Drawing a square")
    }
}

val circle = Circle()
val square = Square()

circle.draw()  // Output: Drawing a circle
square.draw()  // Output: Drawing a square
  

5. Abstraction

Abstraction allows you to hide complex implementation details and expose only essential features. In Kotlin, abstraction is implemented using abstract classes and abstract methods.

// Abstract class
abstract class Vehicle {
    abstract fun start()
}

// Derived class provides implementation of the abstract method
class Car : Vehicle() {
    override fun start() {
        println("Car is starting.")
    }
}

val myCar = Car()
myCar.start()  // Output: Car is starting.
  

6. Interface

An interface defines a contract that implementing classes must fulfill. In Kotlin, interfaces can contain method declarations without a body.

// Interface
interface Drivable {
    fun drive()
}

// Class implements the interface
class Car : Drivable {
    override fun drive() {
        println("The car is driving.")
    }
}

val car = Car()
car.drive()  // Output: The car is driving.
  

๐Ÿงช Try It Yourself

Here are some challenges for you to practice:

  • Create a Person class with properties for name, age, and occupation, and include a method to introduce themself.
  • Define a Shape class with subclasses like Circle and Rectangle, and implement the draw method.
  • Design an interface called Flyable and implement it in classes like Bird and Airplane.

๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review