A Day in the Magical World of Dart Programming!
ডার্ট (Dart) প্রোগ্রামিংয়ের জাদুকরী দুনিয়ায় এক দিন!
Today was all about getting lost in the magical world of programming! I was actually studying Dart's OOP concepts for an upcoming exam in my "Mobile Platform for IoT Devices" course. As I was studying, a thought crossed my mind: the relatable, real-life examples I use to remember these complex concepts could actually be super helpful for others too. So, let me share the full story of what I learned today and explain these fascinating OOP concepts just the way I memorized them!
1. Class and Object
Real-life example: Before building a house, an engineer draws a blueprint on paper—that is the Class. The actual house built following that blueprint is the Object.
What do they do? They divide massive codebases into smaller, manageable parts. A Class defines what data (like a person's name or age) or properties an object will have, and what actions or methods (like walking, talking) it can perform.
2. Constructor
Real-life example: Imagine buying a new phone. The company pre-installs the battery, screen, or default apps so that it's ready to use right out of the box.
What does it do? In programming, a constructor's job is to automatically set initial values the moment an object is created. This saves the trouble of writing code on separate lines to set values repeatedly.
3. Encapsulation
Real-life example: Think of a medicine capsule. The actual medicine inside the plastic or gel casing is protected from outside dust or bacteria.
What does it do? Its job is Data Hiding—hiding sensitive data inside a class from unwanted external use or modification. In Dart, a variable is made private by placing an underscore _ before it, and access is granted only through Getters/Setters after validating the data (for example, ensuring a product's price isn't accidentally set to negative).
4. Inheritance
Real-life example: In real life, children inherit many traits (like skin color or height) from their parents.
What does it do? It brings code or traits from one class into a new class using the extends keyword. For example, a Dog class can directly inherit all traits from an Animal class (since a Dog is an Animal). This prevents writing the same code repeatedly.
5. Polymorphism and Method Overriding
Real-life example: When you're at university, you're a student. At home with your parents, you're a child, and hanging out with friends, you're a friend. You are the same person, but you change your behavior depending on the situation.
What does it do? It allows objects to behave differently based on the situation. If a child class doesn't like a method from the parent class (e.g., an Animal's eating style), it can use @override to modify it and create its own version (e.g., a Dog's unique eating style).
6. Static Variables and Methods
Real-life example: Think of a notice board in an office. Instead of giving a separate notice to each employee's desk, hanging one board on the wall lets everyone see it.
What does it do? Instead of wasting separate memory space, it keeps a common piece of data for the whole class. It becomes the direct property of the entire class, so all objects share this single variable (like counting the total number of Employees in a company).
7. Enum
Real-life example: The 7 days of the week or genders (Male/Female)—these are entirely fixed and never change.
What does it do? It organizes specific or fixed values in one place. As a result, nobody can mistakenly input invalid data (like spelling "Male" incorrectly as "Maaale") in the code.
8. Abstract Class
Real-life example: All banks (like Sonali Bank, DBBL) have some common tasks, like calculating interest rates. However, every bank's interest rate is different. So, the central bank just provides a rulebook or blueprint.
What does it do? It creates a guideline or an incomplete template for other classes. You cannot directly create an object of this class, but other classes follow it to complete their own code.
9. Interface and Multiple Inheritance
Real-life example: When you sign an employment contract with a company, you must strictly follow every single rule in that contract.
What does it do? In Dart, you cannot directly inherit traits from multiple classes at once (Multiple Inheritance). So, you use the implements keyword to enter into a contract with another class. This means the child class must completely rewrite all methods of that interface from scratch inside itself.
10. Mixin
Real-life example: Suppose you have different abilities—the ability to fly (CanFly) and walk (CanWalk). A bird needs both abilities, but a human only needs the ability to walk.
What does it do? Without going through the strict rules or contracts of inheritance, it allows you to directly borrow or plug in abilities/code created in another class using the with keyword. This reduces code duplication.
11. Generics
Real-life example: A magic box that has no label on it. Whenever you want, you can turn it into a fruit box, or a book box.
What does it do? It acts as a placeholder like <T> without specifying the data type in advance. As a result, you can use the same logic class to store int, String, or any data type (e.g., List<int>).
12. Null Safety and Late Keyword
Real-life example: Imagine going to a shop and saying, "Brother, give me the product, I promise I'll pay the price a little later."
What does it do? Dart's Sound Null Safety ensures the app doesn't crash because a variable's value is empty or Null. And with the late keyword, you promise Dart: "I'm not providing the value right now, but I will definitely assign a value before it is used."
13. Asynchronous Programming
Real-life example: When you order food at a restaurant, you don't stand still like a statue at the counter while the chef cooks. You sit at your table and use your phone. When the food is ready, you are notified.
What does it do? It allows time-consuming tasks in the code (like fetching data from the internet) to run in the background without freezing or blocking the entire program. In Dart, this brilliant non-blocking task is handled using Future, async, and await.
Overall, today's journey was all about building a solid foundation for creating beautiful and perfect applications. These concepts will be my greatest weapons on the path to building bigger, real-life projects in the future!
আজকের দিনটা ছিল প্রোগ্রামিংয়ের এক দারুণ জাদুকরী দুনিয়ায় হারিয়ে যাওয়ার দিন! আমি মূলত আমার "Mobile Platform for IoT Devices" কোর্সের একটা পরীক্ষার জন্য ডার্ট-এর এই কনসেপ্টগুলো পড়ছিলাম। পড়তে পড়তেই হঠাৎ মাথায় একটা আইডিয়া এল—আমি ঠিক যেভাবে বাস্তব জীবনের উদাহরণ দিয়ে কঠিন এই কনসেপ্টগুলো মনে রেখেছি, তোমাদের সাথেও সেই উদাহরণগুলো শেয়ার করি না কেন! চলো, আজকে আমি কী কী শিখলাম তার পুরো গল্পটা ঠিক সেভাবেই শেয়ার করি!
১. Class এবং Object
বাস্তব জীবনের উদাহরণ: একটা বাড়ি বানানোর আগে ইঞ্জিনিয়ার কাগজে যে নকশা (Blueprint) আঁকেন, সেটা হলো Class। আর ওই নকশা দেখে বাস্তবে যে বাড়িটি তৈরি হয়, সেটাই হলো Object।
এদের কাজ কী? বিশাল কোডকে ছোট ছোট অংশে ভাগ করা। ক্লাস নির্ধারণ করে দেয় যে একটি অবজেক্টের কী কী ডেটা (যেমন: মানুষের নাম, বয়স) বা প্রোপার্টি থাকবে এবং সে কী কাজ বা মেথড (যেমন: হাঁটা, কথা বলা) করতে পারবে।
২. Constructor
বাস্তব জীবনের উদাহরণ: ধরো, তুমি বাজার থেকে একটা নতুন ফোন কিনলে। কোম্পানি থেকেই ফোনে ব্যাটারি, স্ক্রিন বা ডিফল্ট কিছু অ্যাপ সেট করে দেওয়া থাকে, যাতে খোলার সাথে সাথেই ব্যবহার করা যায়।
এটার কাজ কী? প্রোগ্রামিংয়ে অবজেক্ট তৈরি হওয়ার সাথে সাথেই স্বয়ংক্রিয়ভাবে তার ভেতরে প্রাথমিক ভ্যালু (Initial values) সেট করে দেওয়া হলো কনস্ট্রাক্টরের কাজ। এর ফলে বারবার আলাদা লাইনে কোড লিখে ভ্যালু সেট করার কষ্ট কমে যায়।
৩. Encapsulation
বাস্তব জীবনের উদাহরণ: ওষুধের ক্যাপসুলের কথা ভাবো। ক্যাপসুলের প্লাস্টিক বা জেলের আবরণের ভেতরে থাকা আসল ওষুধ বাইরের ধুলোবালি বা ব্যাকটেরিয়া থেকে সুরক্ষিত থাকে।
এটার কাজ কী? ক্লাসের ভেতরের সংবেদনশীল ডেটাগুলোকে বাইরের অনাকাঙ্ক্ষিত ব্যবহার বা পরিবর্তন থেকে লুকিয়ে রাখাই (Data Hiding) এর কাজ। ডার্টে ভেরিয়েবলের আগে আন্ডারস্কোর _ বসিয়ে এটিকে প্রাইভেট করা হয় এবং Getter/Setter এর মাধ্যমে ডেটা চেক করে (যেমন: পণ্যের দাম যেন ভুলে নেগেটিভ না হয়) তারপর অ্যাক্সেস দেওয়া হয়।
৪. Inheritance
বাস্তব জীবনের উদাহরণ: বাস্তব জীবনে বাবা-মায়ের অনেক বৈশিষ্ট্য (যেমন: গায়ের রঙ বা উচ্চতা) সন্তানরা উত্তরাধিকার সূত্রে পেয়ে থাকে।
এটার কাজ কী? এক ক্লাসের তৈরি করা কোড বা বৈশিষ্ট্য extends কিওয়ার্ডের মাধ্যমে নতুন ক্লাসে নিয়ে আসা। যেমন Dog ক্লাস চাইলে Animal ক্লাসের সব গুণ সরাসরি পেয়ে যায় (Dog is an Animal)। এর ফলে একই কোড বারবার লিখতে হয় না।
৫. Polymorphism এবং Method Overriding
বাস্তব জীবনের উদাহরণ: তুমি নিজে যখন ভার্সিটিতে আছো, তখন তুমি একজন ছাত্র। আবার বাসায় বাবা-মায়ের কাছে সন্তান, আর বন্ধুদের আড্ডায় তুমি একজন বন্ধু। মানুষ তুমি একটাই, কিন্তু পরিস্থিতি অনুযায়ী রূপ বদলাচ্ছ।
এটার কাজ কী? অবজেক্টের পরিস্থিতি অনুযায়ী আলাদা আচরণ করা। প্যারেন্ট ক্লাসের কোনো কাজ বা মেথড (যেমন: Animal-এর খাওয়ার স্টাইল) যদি চাইল্ড ক্লাসের পছন্দ না হয়, তবে সে @override ব্যবহার করে সেটাকে মডিফাই করে নিজের মতো করে তৈরি করে নিতে পারে (যেমন: Dog-এর খাওয়ার স্টাইল আলাদা)।
৬. Static Variable এবং Methods
বাস্তব জীবনের উদাহরণ: একটা অফিসের নোটিশ বোর্ডের কথা ভাবো। প্রতিটা কর্মচারীর ডেস্কে আলাদা নোটিশ না দিয়ে দেয়ালে একটা বোর্ড টানিয়ে দিলে সবাই সেটা দেখতে পায়।
এটার কাজ কী? মেমোরিতে আলাদা আলাদা জায়গা নষ্ট না করে ক্লাসের সবার জন্য একটা কমন ডেটা রাখা। এটি সরাসরি পুরো ক্লাসের সম্পত্তি হয়ে যায়, তাই সব অবজেক্ট মিলে এই একটা ভেরিয়েবলই শেয়ার করে (যেমন: কোম্পানিতে মোট কতজন Employee আছে তা গোনা)।
৭. Enum
বাস্তব জীবনের উদাহরণ: সপ্তাহের ৭ দিন বা জেন্ডার (Male/Female)—এগুলো একদম ফিক্সড, কখনো পরিবর্তন হয়বিধা হয় না।
এটার কাজ কী? নির্দিষ্ট বা ফিক্সড কিছু ভ্যালুকে এক জায়গায় সুন্দর করে গুছিয়ে রাখা। এর ফলে কোডে কেউ ভুল করে উল্টাপাল্টা কিছু (যেমন: Male-এর জায়গায় বানান ভুল করে "Maaale") ইনপুট দিতে পারে না।
৮. Abstract Class
বাস্তব জীবনের উদাহরণ: সব ব্যাংকের (যেমন Sonali Bank, DBBL) কিছু কমন কাজ থাকে, যেমন সুদের হার হিসাব করা। কিন্তু একেক ব্যাংকের সুদের হার একেক রকম। তাই সেন্ট্রাল ব্যাংক শুধু একটা রুলবুক বা ব্লুপ্রিন্ট দিয়ে দেয়।
এটার কাজ কী? অন্যান্য ক্লাসের জন্য একটা গাইডলাইন বা অসম্পূর্ণ ছাঁচ তৈরি করা। এই ক্লাসের সরাসরি কোনো অবজেক্ট বানানো যায় না, কিন্তু অন্য ক্লাসগুলো একে ফলো করে নিজেদের কোড সম্পূর্ণ করে।
৯. Interface এবং Multiple Inheritance
বাস্তব জীবনের উদাহরণ: একটা কোম্পানিতে কাজ করার জন্য যখন চুক্তিনামা সাইন করা হয়, তখন চুক্তির প্রতিটা নিয়ম অক্ষরে অক্ষরে পালন করতে হয়।
এটার কাজ কী? ডার্টে একসাথে একাধিক ক্লাস থেকে সরাসরি বৈশিষ্ট্য ধার করা (Multiple Inheritance) যায় না। তাই implements কিওয়ার্ড ব্যবহার করে অন্য ক্লাসের সাথে চুক্তিতে আবদ্ধ হওয়া যায়। এর মানে হলো ওই ইন্টারফেসের সব মেথড চাইল্ড ক্লাসকে নিজের ভেতর একদম নতুন করে লিখতে হবে।
১০. Mixin
বাস্তব জীবনের উদাহরণ: ধরো তোমার কাছে বিভিন্ন ক্ষমতা আছে—উড়তে পারা (CanFly) এবং হাঁটতে পারা (CanWalk)। একটা পাখির এই দুইটা ক্ষমতাই লাগে, কিন্তু মানুষের শুধু হাঁটার ক্ষমতা লাগে।
এটার কাজ কী? ইনহেরিটেন্সের কড়া নিয়ম বা চুক্তির ঝামেলায় না গিয়ে সরাসরি অন্য ক্লাসের তৈরি করা ক্ষমতা বা কোড with কিওয়ার্ড দিয়ে প্লাগ-ইন বা ধার করা। এতে কোডের ডুপ্লিকেশন কমে।
১১. Generics
বাস্তব জীবনের উদাহরণ: একটা জাদুর বাক্স, যার গায়ে আগে থেকে কিছু লেখা নেই। তুমি যখন চাইবে তখন সেটাকে ফলের বাক্স বানাতে পারো, আবার চাইলে বইয়ের বাক্স বানাতে পারো।
এটার কাজ কী? কোডে ডেটা টাইপ আগে থেকে নির্দিষ্ট না করে <T> চিহ্নের মতো একটা প্লেসহোল্ডার বসিয়ে দেওয়া। ফলে একই লজিকের ক্লাস দিয়ে তুমি int, String বা যেকোনো ধরনের ডেটা রাখতে পারো (যেমন: List<int>)।
১২. Null Safety এবং Late Keyword
বাস্তব জীবনের উদাহরণ: ধরো তুমি দোকানে গিয়ে বললে, "ভাই আমাকে পণ্যটা দিন, আমি কথা দিচ্ছি দামটা একটু পরে দেব।"
এটার কাজ কী? কোনো ভেরিয়েবলের ভ্যালু খালি বা Null থাকার কারণে যেন অ্যাপ ক্র্যাশ না করে, তা নিশ্চিত করে ডার্টের Sound Null Safety। আর late কিওয়ার্ড দিয়ে ডার্টকে কথা দেওয়া হয় যে, "ভেরিয়েবলের ভ্যালু এখন দিচ্ছি না, কিন্তু এটি ব্যবহারের আগে ঠিকই ভ্যালু অ্যাসাইন করে দেব"।
১৩. Asynchronous Programming
বাস্তব জীবনের উদাহরণ: একটা রেস্টুরেন্টে খাবার অর্ডার দিয়ে বাবুর্চি যতক্ষণ রান্না করছে, তুমি কিন্তু কাউন্টারের সামনে স্ট্যাচু বা ব্লক হয়ে দাঁড়িয়ে থাকো না। তুমি নিজের জায়গায় বসে মোবাইল চালাও। খাবার রেডি হলে তোমাকে জানানো হয়।
এটার কাজ কী? কোডের কোনো সময়সাপেক্ষ কাজ (যেমন ইন্টারনেট থেকে ডেটা লোড হওয়া) আসলে সেটার জন্য পুরো প্রোগ্রামকে আটকে বা ফ্রিজ না করে ব্যাকগ্রাউন্ডে চলতে দেওয়া। ডার্টে Future, async এবং await ব্যবহার করে এই চমৎকার নন-ব্লকিং কাজটি করা হয়।
সব মিলিয়ে আজকের এই জার্নিটা ছিল একটা সুন্দর এবং পারফেক্ট অ্যাপ্লিকেশন তৈরির ভিত্তি মজবুত করার দিন। আগামীতে আরো বড় এবং রিয়েল-লাইফ প্রজেক্ট বানানোর পথে এই কনসেপ্টগুলোই হবে আমার সবচেয়ে বড় হাতিয়ার!