Kotlin Polymorphism – ব্রেইন-ফ্রেন্ডলি ব্যাখ্যা!

 

🎯 Kotlin Polymorphism – ব্রেইন-ফ্রেন্ডলি ব্যাখ্যা!

🧠 Polymorphism মানে কী?

👉 শব্দটি এসেছে "Poly" (অনেক) + "Morph" (রূপ) = "অনেক রূপ" 🤯
👉 একই জিনিস আলাদা আলাদা রূপে কাজ করতে পারে!

Example:

  • 🐶 Dog আর 🐱 Cat দুটোই Animal – কিন্তু তারা ভিন্নভাবে শব্দ করে!
  • 🎸 Guitar আর 🎹 Piano দুটোই Instrument – কিন্তু তারা ভিন্নভাবে বাজে!
  • 🚗 Car আর 🏍️ Bike দুটোই Vehicle – কিন্তু চালানোর ধরন আলাদা!

Kotlin-এ Polymorphism মেইনলি দুইভাবে হয়:
1️⃣ Method Overriding (Runtime Polymorphism)
2️⃣ Method Overloading (Compile-time Polymorphism)


🎯 1️⃣ Method Overriding – যখন চাইল্ড ক্লাস তার নিজের মতো করে মেথড বদলায়!

👉 একই মেথড Parent এবং Child-এ আলাদা ভাবে কাজ করবে!

Example: Animal → Dog & Cat

// ✅ Parent Class: Animal
open class Animal {
    open fun makeSound() {
        println("Some animal sound... 🔊")
    }
}

// ✅ Child Class: Dog
class Dog : Animal() {
    override fun makeSound() {
        println("Bark! 🐶")
    }
}

// ✅ Child Class: Cat
class Cat : Animal() {
    override fun makeSound() {
        println("Meow! 🐱")
    }
}

fun main() {
    val myDog: Animal = Dog()  // ✅ Animal টাইপের Dog!
    val myCat: Animal = Cat()  // ✅ Animal টাইপের Cat!

    myDog.makeSound()  // ✅ Output: Bark! 🐶
    myCat.makeSound()  // ✅ Output: Meow! 🐱
}

Output:

Bark! 🐶
Meow! 🐱

👉 Parent ক্লাসে makeSound() ছিল, কিন্তু Dog & Cat নিজেদের মতো করে ওভাররাইড করেছে! 😍


🎯 2️⃣ Method Overloading – এক নামের মেথড, কিন্তু ভিন্ন Parameter!

👉 একই মেথড-এর আলাদা আলাদা Parameter থাকলে, Kotlin বুঝবে কোনটা চালাতে হবে!

Example: Calculator – যোগ, গুণ!

class Calculator {
    fun add(a: Int, b: Int): Int {
        return a + b
    }

    fun add(a: Double, b: Double): Double {
        return a + b
    }

    fun add(a: Int, b: Int, c: Int): Int {
        return a + b + c
    }
}

fun main() {
    val calc = Calculator()

    println(calc.add(5, 10))         // ✅ 15 (Int)
    println(calc.add(5.5, 10.5))     // ✅ 16.0 (Double)
    println(calc.add(5, 10, 15))     // ✅ 30 (3 Numbers)
}

Output:

15
16.0
30

👉 একই add() মেথড তিনবার লিখেছি, কিন্তু Kotlin ঠিকই বুঝে নিয়েছে কোনটা রান করতে হবে! 😎


🎯 3️⃣ Polymorphism in Action – Shape Example!

👉 একই draw() মেথড, কিন্তু প্রতিটা Shape আলাদা ভাবে আঁকে!

Example: Shape → Circle & Rectangle

// ✅ Parent Class: Shape
open class Shape {
    open fun draw() {
        println("Drawing a shape 🖌️")
    }
}

// ✅ Child Class: Circle
class Circle : Shape() {
    override fun draw() {
        println("Drawing a Circle ⚪")
    }
}

// ✅ Child Class: Rectangle
class Rectangle : Shape() {
    override fun draw() {
        println("Drawing a Rectangle ▭")
    }
}

fun main() {
    val shapes: List<Shape> = listOf(Circle(), Rectangle())

    for (shape in shapes) {
        shape.draw()  // ✅ একই মেথড, কিন্তু আলাদা কাজ করছে!
    }
}

Output:

Drawing a Circle ⚪
Drawing a Rectangle ▭

👉 একই draw() মেথড, কিন্তু আলাদা Shape ভিন্নভাবে কাজ করছে! 🎨


🎯 সংক্ষেপে:

বিষয় ব্যাখ্যা Example
Method Overriding Parent-এর মেথডকে Child নিজের মতো করে পরিবর্তন করে override fun makeSound()
Method Overloading একই নাম, কিন্তু আলাদা Parameter fun add(a: Int, b: Int) & fun add(a: Double, b: Double)
Runtime Polymorphism Parent টাইপের অবজেক্ট, কিন্তু Child-এর মেথড রান হয় val myDog: Animal = Dog()

🚀 এখন আপনি Kotlin Polymorphism মাস্টার! 🏆

👉 এবার কোড প্র্যাকটিস শুরু করে দিন! 😎🔥

Comments

Popular posts from this blog

AI (Artificial Intelligence) সহজভাবে বোঝার জন্য একদম মস্তিষ্ক-বান্ধব ব্যাখ্যা 🧠🤖

🧠 Regex কী?

Kotlin-এ ভেরিয়েবল ঘোষণা করার দুই উপায়