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

 

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

🧠 Constructor মানে কী?

👉 Constructor হল কোনো ক্লাসের জন্য "গেটওয়ে" – এটা অবজেক্ট তৈরি হওয়ার সময় প্রথমে কল হয়!
👉 ক্লাস যখন অবজেক্ট হয়, তখন Constructor সেটআপের কাজ করে!
👉 এটা ভ্যালু ইনিশিয়ালাইজ (initialize) করতে ব্যবহার হয়!

Example:

  • 🏠 বাসা বানানোর সময়ই দরজা-জানালা ঠিক করতে হয়!
  • 🚗 গাড়ি কেনার সময়ই এর কালার, মডেল ঠিক করা হয়!
  • 👶 একটি বাচ্চার জন্মের সময়ই তার নাম ও বয়স সেট করা হয়!

Kotlin-এ দুই ধরনের Constructor থাকে:
1️⃣ Primary Constructor – ক্লাসের নামের সাথেই থাকে!
2️⃣ Secondary Constructorconstructor কীওয়ার্ড দিয়ে আলাদা লেখা হয়!


🎯 1️⃣ Primary Constructor – ক্লাসের নামের সাথেই থাকে!

👉 Constructor সরাসরি ক্লাসের সাথে ডিফাইন করা হয়!

Example: Person (নাম ও বয়স সেট করা)

class Person(val name: String, val age: Int) {
    fun displayInfo() {
        println("Name: $name, Age: $age")
    }
}

fun main() {
    val person1 = Person("Alice", 25)
    person1.displayInfo()  // ✅ Output: Name: Alice, Age: 25

    val person2 = Person("Bob", 30)
    person2.displayInfo()  // ✅ Output: Name: Bob, Age: 30
}

Output:

Name: Alice, Age: 25
Name: Bob, Age: 30

👉 Constructor কল হলেই নাম- বয়স সেট হয়ে যাবে! 😍


🎯 2️⃣ Initializer Block (init) – Extra কাজ করতে চাইলে!

👉 Primary Constructor-এর সাথে যদি কিছু এক্সট্রা কোড রান করতে হয়, তাহলে init ব্লক ব্যবহার করুন!

Example: Message Show করার জন্য!

class Student(val name: String, val roll: Int) {
    init {
        println("Student Created: $name, Roll: $roll")
    }
}

fun main() {
    val student1 = Student("John", 101)
    val student2 = Student("Emma", 102)
}

Output:

Student Created: John, Roll: 101
Student Created: Emma, Roll: 102

👉 init ব্লক তখনই কল হয়, যখন অবজেক্ট তৈরি হয়! 😎


🎯 3️⃣ Secondary Constructor – আলাদা constructor ব্লক!

👉 যদি Primary Constructor দরকার না হয়, তাহলে constructor দিয়ে আলাদা তৈরি করা যায়!

Example: Car (Primary + Secondary Constructor)

class Car(val brand: String, val model: String) {
    var year: Int = 0  // Default value

    // ✅ Secondary Constructor
    constructor(brand: String, model: String, year: Int) : this(brand, model) {
        this.year = year
    }

    fun showInfo() {
        println("Car: $brand $model, Year: $year")
    }
}

fun main() {
    val car1 = Car("Toyota", "Corolla")
    car1.showInfo()  // ✅ Output: Car: Toyota Corolla, Year: 0

    val car2 = Car("Honda", "Civic", 2023)
    car2.showInfo()  // ✅ Output: Car: Honda Civic, Year: 2023
}

Output:

Car: Toyota Corolla, Year: 0
Car: Honda Civic, Year: 2023

👉 Secondary Constructor প্রাইমারি Constructor কে কল করে!


🎯 4️⃣ Default Value – Constructor কে সহজ করা!

👉 যদি কোনো ভ্যালু না দেওয়া হয়, তাহলে ডিফল্ট ভ্যালু সেট করতে পারেন!

Example: Default Age!

class Human(val name: String, val age: Int = 18) {
    fun show() {
        println("Name: $name, Age: $age")
    }
}

fun main() {
    val person1 = Human("Alice")  // Age 18 ধরে নেবে!
    person1.show()  // ✅ Output: Name: Alice, Age: 18

    val person2 = Human("Bob", 25)
    person2.show()  // ✅ Output: Name: Bob, Age: 25
}

Output:

Name: Alice, Age: 18
Name: Bob, Age: 25

👉 Constructor-এর age এর ডিফল্ট ভ্যালু ১৮ ধরে নিয়েছে! 😍


🎯 সংক্ষেপে:

Constructor ব্যাখ্যা Example
Primary Constructor ক্লাসের নামের সাথেই থাকে class Person(val name: String, val age: Int)
Initializer Block (init) Constructor এর সাথে Extra কোড রান করতে init { println("Created!") }
Secondary Constructor আলাদা constructor ব্লক দিয়ে লেখা হয় constructor(brand: String, year: Int) : this(brand) { }
Default Value Default Parameter দিলে ভ্যালু না দিলেও চলবে class Human(val name: String, val age: Int = 18)

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

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

Comments

Popular posts from this blog

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

🧠 Regex কী?

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