Published on

OOP Mastery: Polymorphism in Kotlin

Authors

OOP Mastery is a series of self-study articles on Object-Oriented Programming with the Kotlin language, compiled at DanTech0xFF. The purpose of this series of articles is to provide a comprehensive view of Object-Oriented Programming in the Kotlin language. This will be a solid foundation for future programmers if you delve into Android, Mobile Cross Platform, or Backend Java.

  • Part 1: OOP Mastery: Types of Classes, Interfaces in Kotlin
  • Part 2: OOP Mastery: Encapsulation in Kotlin
  • Part 3: OOP Mastery: Inheritance in Kotlin
  • Part 4: OOP Mastery: Polymorphism in Kotlin
  • Part 5: OOP Mastery: Abstraction in Kotlin

In this article, we will analyze the Polymorphism of OOP in the Kotlin programming language.

Remind about Polymorphism

Polymorphism in OOP is expressed through the fact that different Classes inheriting the same Class or Interface can call the same function (the term is called the same interface), but can perform different Logic depending on each declaration location.

Simply put, Polymorphism - the same form but different content inside.

Polymorphism in Kotlin

Overload

Overload is the definition of methods (functions) in a Class with the same name but different input params

interface Encryptor {
  fun encrypt(input: String): String
  fun encrypt(input: Int): Int
  fun encrypt(input: Char): Char
}

Override

Override: Defining methods (functions) in a child Class that are inherited from the parent Class, and completely changing the logic inside it.

  • Override with open class
open class BaseClass {
    open var name: String = "BaseClass"
    open fun print() {
        println("Hello from BaseClass1")
    }
}

class SubClass : BaseClass() {
    override var name: String = "SubClass" // kotlin allows you to override properties
        get() = super.name
        set(value) {
            field = value
        }
    override fun print() {
        super.print() // -> call hàm của lớp BaseClass
        println("Hello from SubClass")
    }
}
  • Override with Interface
interface MyInterface {
    val name: String // interface của Kotlin có thể chứa member variable
    fun myFunction() {
    // interface của Kotlin có thể chứa hàm đã được định nghĩa sẵn
        println("Hello from MyInterface")
    }
    // interface của Kotlin có thể chứa hàm chưa được định nghĩa
    fun myEmptyFunction()
}

class MyClass : MyInterface {
// cách override trong Implementation của Interface
    override val name: String = "MyClass"
    override fun myFunction() {
        super.myFunction()
        println("Hello from MyClass")
    }

    override fun myEmptyFunction() {
        println("Hello from myEmptyFunction")
    }
}
  • Override with abstract class
abstract class MyAbstractClass {
    open val name: String = "Name"
    abstract val age: Int
    abstract fun myFunction()
    open fun myOpenFunction() {
        println("Hello from myEmptyFunction")
    }
    fun myNormalFunction() {
        println("Hello from myNormalFunction")
    }
}

class MySubClass : MyAbstractClass() {
    override val age: Int = 10
    override fun myFunction() {
        println("Hello from myFunction")
    }

    override fun myOpenFunction() {
        super.myOpenFunction()
    }

    override val name: String
        get() = super.name
}

Questions when applying Polymorphism in Kotlin

Question 1: What is the difference between inheriting from a Class and an Interface?

Answer: A Class can only inherit from 1 Class, but can implement multiple Interfaces.

Question 2: When you don't want external modules to inherit the Class or Interface, what do you need to do?

Answer 2: Use sealed Class, Interface

Question 3: In Kotlin, what is the difference between abstract class and class?

Answer 3: class can be used to create objects directly, abstract class cannot.

Note: Interface and abstract Class can still create objects through object expression. This method should not be applied to large interfaces or abstract classes. Because it will make the code complex, difficult to read, and difficult to debug.

Good luck!