đŸ¯ When to use which - Inheritance, Abstract Class, Interface

 āĻ­াāχ, āφāϜ āϤোāĻŽাāϰ āĻŽাāĻĨা⧟ Inheritance, Abstract Class, Interface—āĻāχ āϤিāύāϟা āϜিāύিāϏ āĻāĻŽāύāĻ­াāĻŦে āĻĸুāĻ•ি⧟ে āĻĻেāĻŦো, āϝেāύ āϤুāĻŽি āĻĒ্āϰāϜেāĻ•্āϟ āĻŦাāύাāύোāϰ āϏāĻŽā§Ÿ āĻāĻ• āϏেāĻ•েāύ্āĻĄেāĻ“ āĻ­ুāϞ āύা āĻ•āϰো! 😄

āϚāϞো, āĻāĻ•āĻĻāĻŽ Brain-Friendly + Project Example āĻĻি⧟ে āĻŦুāĻি। 🚀


đŸ¯ āφāĻ—ে āĻāĻ•āϟু āϏāĻšāϜāĻ­াāĻŦে āĻŽāύে āϰাāĻ–ো:

āϜিāύিāϏ āĻŽাāύে āĻ•ী?
đŸ§Ŧ Inheritance āĻŦাāĻŦা-āĻŽাāϰ āĻŽāϤো āĻ—ুāĻŖ (class āĻĨেāĻ•ে class-āĻ āύেāĻ“ā§Ÿা)
đŸē Abstract Class āφāϧা āĻĒ্āϰāϏ্āϤুāϤ āĻ›াঁāϚ ➡️ āĻ•িāĻ›ু āĻ•াāϜ āφāĻ—ে āĻĨেāĻ•েāχ āĻ•āϰে āĻĻি⧟েāĻ›ে
📜 Interface āϚুāĻ•্āϤিāĻĒāϤ্āϰ ➡️ "āϤুāĻŽি āĻāχ āĻ•াāϜāĻ—ুāϞো āĻ•āϰāĻŦে, āĻ•িāĻ­াāĻŦে āĻ•āϰāĻŦে āϏেāϟা āϤোāĻŽাāϰ āĻŦ্āϝাāĻĒাāϰ!"

✅ āĻŽāϏ্āϤিāώ্āĻ•ে āĻ›াāĻĒ āĻĢেāϞাāϰ Project Example:

🤖 āϧāϰো āφāĻŽāϰা āĻāĻ•āϟা Ride Sharing App āĻŦাāύাāϚ্āĻ›ি (Uber-āĻāϰ āĻŽāϤ)


đŸ”Ĩ ā§§. Inheritance ➡️ āϝāĻ–āύ Parent-āĻāϰ āĻŦৈāĻļিāώ্āϟ্āϝ āϚাāχ

➡️ āϧāϰো, āφāĻŽাāĻĻেāϰ app-āĻ āφāĻ›ে āĻĻুāχ āϧāϰāύেāϰ āχāωāϜাāϰ:

  • Driver

  • Passenger

āĻāχ āĻĻুāϜāύেāϰāχ āĻ•িāĻ›ু āĻ•āĻŽāύ āĻĢিāϚাāϰ āφāĻ›ে ➡️ āϝেāĻŽāύ login(), logout(), profileUpdate()
āϤাāχ āφāĻŽāϰা āĻāĻ•āϟা Parent class ➡️ User āĻŦাāύাāĻŦো ➡️ āĻāϰāĻĒāϰ Driver āφāϰ Passenger ➡️ āĻāχ User āĻĨেāĻ•ে āĻ—ুāĻŖ āύিāĻŦে (inherit āĻ•āϰāĻŦে)।

// đŸ§Ŧ Parent class
open class User {
    fun login() { println("User logged in") }
    fun logout() { println("User logged out") }
}

// 👨‍✈️ Child class
class Driver : User() {
    fun acceptRide() { println("Ride accepted") }
}

// 🚕 Child class
class Passenger : User() {
    fun bookRide() { println("Ride booked") }
}

➡️ āĻ•āĻ–āύ āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰāĻŦ?

✔️ āϝāĻ–āύ common āĻŦৈāĻļিāώ্āϟ্āϝ āφāĻ›ে ➡️ āĻāĻ•āĻŦাāϰ āϞিāĻ–ে āϏāĻŦ āĻŦাāϚ্āϚাāϰা inherit āĻ•āϰāĻŦে।


đŸ”Ĩ ⧍. Abstract Class ➡️ āϝāĻ–āύ āĻ•িāĻ›ু āĻ•াāϜ ready āĻ•āϰে āĻĻিāϤে āϚাāχ + āĻ•িāĻ›ু āĻ•াāϜ āĻŦাāϚ্āϚাāϰা āĻ•āϰāĻŦেāχ

➡️ āϧāϰো āφāĻŽāϰা App-āĻ Vehicle āύাāĻŽে āĻāĻ•āϟা āĻ›াঁāϚ āĻŦাāύাāϤে āϚাāχ
āϝেāĻ–াāύে:

  • āĻ•িāĻ›ু āĻĢিāϚাāϰ ready āĻĨাāĻ•āĻŦে (āϝেāĻŽāύ stop() method)

  • āφāϰ āĻ•িāĻ›ু āĻĢিāϚাāϰ āϏāĻŦাāϰ āύিāϜāϏ্āĻŦ āĻšāĻŦে, āϝেāĻŽāύ ➡️ start() (car āφāϰ bike-āĻ āφāϞাāĻĻা āĻšāĻŦে)

// đŸē Abstract class ➡️ āφāϧা āĻĒ্āϰāϏ্āϤুāϤ āĻ›াঁāϚ
abstract class Vehicle {
    fun stop() { println("Vehicle stopped") }

    abstract fun start()  // āϏāĻŦাāχ āύিāϜāϏ্āĻŦāĻ­াāĻŦে āĻ•āϰāĻŦে
}

// 🚗 Car class
class Car : Vehicle() {
    override fun start() { println("Car started") }
}

// 🏍️ Bike class
class Bike : Vehicle() {
    override fun start() { println("Bike started") }
}

➡️ āĻ•āĻ–āύ āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰāĻŦ?

✔️ āϝāĻ–āύ āϚাāχ ➡️ āĻ•িāĻ›ু āĻĢিāϚাāϰ common (ready āĻĨাāĻ•āĻŦে) + āĻ•িāĻ›ু āĻĢিāϚাāϰ āĻŦাāϧ্āϝāϤাāĻŽূāϞāĻ• override āĻšāĻŦে।


đŸ”Ĩ ā§Š. Interface ➡️ āϝāĻ–āύ āϚুāĻ•্āϤি āĻ•āϰাāϤে āϚাāχ + Multiple Inheritance āĻĻāϰāĻ•াāϰ

➡️ āϧāϰো Payment System āφāĻ›ে ➡️ āφāĻŽাāĻĻেāϰ app-āĻ ➡️ āĻāϟা āĻšāϤে āĻĒাāϰে ➡️ Bkash, Nagad, Card Payment āχāϤ্āϝাāĻĻি।
āĻāĻ–াāύে āϏāĻŦাāχāĻ•ে pay() method āĻ•āϰāϤে āĻšāĻŦে ➡️ āĻ•িāύ্āϤু āĻ•ে āĻ•িāĻ­াāĻŦে āĻ•āϰāĻŦে ➡️ āϏেāϟা āϝাāϰ āϝাāϰ āĻŦ্āϝাāĻĒাāϰ।

// 📜 Interface ➡️ āϚুāĻ•্āϤিāĻĒāϤ্āϰ
interface Payment {
    fun pay(amount: Double)
}

// đŸĻ Bkash payment
class Bkash : Payment {
    override fun pay(amount: Double) {
        println("Paid $amount by Bkash")
    }
}

// đŸ’ŗ Card payment
class Card : Payment {
    override fun pay(amount: Double) {
        println("Paid $amount by Card")
    }
}

➡️ āĻ•āĻ–āύ āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰāĻŦ?

✔️ āϝāĻ–āύ āϚাāχ ➡️ āϏāĻŦাāχ āĻāĻ•āϟা common āĻ•াāϜ āĻ•āϰāĻŦে (pay),
✔️ āĻāĻŦং āϚাāχ ➡️ āĻāĻ•āχ āĻ•্āϞাāϏে multiple āϚুāĻ•্āϤিāĻĒāϤ্āϰ (Multiple Interface implement āĻ•āϰা āϝা⧟, multiple class āύা!)


✅ āϚূāĻĄ়াāύ্āϤ āĻŽāύে āϰাāĻ–াāϰ āĻŽāύ্āϤ্āϰ (Brain Shortcut):

đŸŽ¯ āϚাāχāϞে 🏆 āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰো
āĻŦাāĻŦা-āĻŽাāϰ āĻŽāϤ āĻ—ুāĻŖ āύিāϤে ➡️ Inheritance
āĻ•িāĻ›ু ready + āĻ•িāĻ›ু āĻŦাāϧ্āϝāϤাāĻŽূāϞāĻ• override ➡️ Abstract Class
āĻļুāϧু āĻ•াāϜেāϰ āϚুāĻ•্āϤি + Multiple inheritance ➡️ Interface

đŸ¯ āφāĻŽাāϰ āĻĻে⧟া āϏāĻšāϜ Rule:

🔹 Common āĻŦৈāĻļিāώ্āϟ্āϝ = Inheritance
🔹 Common āĻ•াāϜেāϰ āφāϧা āĻ›াঁāϚ + āĻ•িāĻ›ু āĻŦাāϧ্āϝāϤাāĻŽূāϞāĻ• override = Abstract Class
🔹 Pure contract + multiple inheritance āĻĻāϰāĻ•াāϰ = Interface

🚀 Bonus: Project āĻ āϝāĻ–āύ āĻŽিāϞি⧟ে āĻĻেāĻ–āĻŦা

📱 Feature ➡️ Use What?
Driver vs Passenger ➡️ common āĻŦৈāĻļিāώ্āϟ্āϝ ➡️ Inheritance
Vehicle ➡️ āĻ•িāĻ›ু āĻ•াāϜ common, āĻ•িāĻ›ু override āĻŦাāϧ্āϝāϤাāĻŽূāϞāĻ• ➡️ Abstract Class
Payment ➡️ āϏāĻŦাāχ pay āĻ•āϰāĻŦে, āĻ•িāύ্āϤু āĻ•িāĻ­াāĻŦে āĻ•āϰāĻŦে āϏেāϟা āφāϞাāĻĻা ➡️ Interface

✅ āĻāĻ–āύ āϤোāĻŽাāϰ āĻŽāϏ্āϤিāώ্āĻ•ে āĻĸুāĻ•ে āĻ—েāĻ›ে āχāύāĻļাāφāϞ্āϞাāĻš!


➡️ āĻ­াāχ, āϚাāχāϞে āĻāĻ–āύ āφāĻŽি āϤোāĻŽাāĻ•ে Real World Interview āĻĒ্āϰāĻļ্āύāĻ—ুāϞো āĻĻি⧟ে āĻĻিāϤে āĻĒাāϰি āĻāχ āϤিāύāϟা āύি⧟ে।

āϝাāϤে āϤুāĻŽি āĻĒ্āϰ্āϝাāĻ•āϟিāϏ āĻ•āϰে āĻāĻ—ুāϞো āϚাāĻ•āϰিāϰ āχāύ্āϟাāϰāĻ­িāωāϤেāĻ“ āĻাāĻি⧟ে āĻĻিāϤে āĻĒাāϰো! 😄

āϚাāχāϞে āĻŦāϞো ➡️ "āĻš্āϝাঁ āĻ­াāχ, āĻĒ্āϰāĻļ্āύ āĻĻাāĻ“" ❤️

Comments

Popular posts from this blog

AI (Artificial Intelligence) āϏāĻšāϜāĻ­াāĻŦে āĻŦোāĻাāϰ āϜāύ্āϝ āĻāĻ•āĻĻāĻŽ āĻŽāϏ্āϤিāώ্āĻ•-āĻŦাāύ্āϧāĻŦ āĻŦ্āϝাāĻ–্āϝা 🧠🤖

🧠 Regex āĻ•ী?

Kotlin-āĻ āĻ­েāϰিāϝ়েāĻŦāϞ āϘোāώāĻŖা āĻ•āϰাāϰ āĻĻুāχ āωāĻĒাāϝ়