Parent Class (সুপার ক্লাস) এবং Child Class (সাব ক্লাস) ব্যবহার করে আপনি অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং (OOP) এর বৈশিষ্ট্যগুলি ব্যবহার করে কোড পুনঃব্যবহার এবং সংগঠনের সুবিধা পেতে পারেন। নিচে Parent এবং Child Class এর ব্যবহার এবং উদাহরণসহ আলোচনা করা হলো।
. Parent Class (সুপার ক্লাস)
Parent Class হল একটি ক্লাস যা অন্যান্য ক্লাসকে বৈশিষ্ট্য এবং আচরণ প্রদান করে। এটি সাধারণ বৈশিষ্ট্য এবং মেথড ধারণ করে।
উদাহরণ:
class Product {
String name;
double price;
Product(this.name, this.price); // Constructor
void displayDetails() {
print("Product Name: $name");
print("Price: \$${price.toStringAsFixed(2)}");
}
}
Child Class (সাব ক্লাস)
Child Class হল একটি ক্লাস যা Parent Class থেকে বৈশিষ্ট্য এবং আচরণ অর্জন করে। এটি Parent Class এর মেথডগুলোকে ওভাররাইড (override) করে নতুনভাবে সংজ্ঞায়িত করতে পারে।
উদাহরণ:
class Electronics extends Product {
int warranty; // Warranty in months
Electronics(String name, double price, this.warranty) : super(name, price); // Constructor
@override
void displayDetails() {
super.displayDetails(); // Call the parent method
print("Warranty: $warranty months");
}
}
class Clothing extends Product {
String size; // Size of the clothing item
Clothing(String name, double price, this.size) : super(name, price); // Constructor
@override
void displayDetails() {
super.displayDetails();
print("Size: $size");
}
}
class Furniture extends Product {
String material; // Material of the furniture
Furniture(String name, double price, this.material) : super(name, price); // Constructor
@override
void displayDetails() {
super.displayDetails();
print("Material: $material");
}
}
Parent এবং Child Class এর ব্যবহার
void main() {
// Create instances of each product type
Product laptop = Electronics("Laptop", 999.99, 24);
Product shirt = Clothing("T-Shirt", 19.99, "M");
Product chair = Furniture("Dining Chair", 89.99, "Wood");
// Store products in a list
List<Product> products = [laptop, shirt, chair];
// Display details of each product
for (var product in products) {
product.displayDetails();
print('---------------------'); // Separator
}
}Multiple Child Classes
একটি Parent Class থেকে একাধিক Child Class তৈরি করা সম্ভব। এটি বিভিন্ন ধরনের অবজেক্ট তৈরিতে সাহায্য করে।
উদাহরণ:
// Parent Class
class Animal {
String name;
Animal(this.name);
String makeSound() {
return "Animal sound";
}
String getName() {
return name;
}
}
// Child Class: Dog
class Dog extends Animal {
Dog(String name) : super(name);
@override
String makeSound() {
return "Bark";
}
}
// Child Class: Cat
class Cat extends Animal {
Cat(String name) : super(name);
@override
String makeSound() {
return "Meow";
}
}
// Child Class: Cow
class Cow extends Animal {
Cow(String name) : super(name);
@override
String makeSound() {
return "Moo";
}
}
void main() {
// Object Creation
var dog = Dog("Buddy");
var cat = Cat("Whiskers");
var cow = Cow("Bessie");
// Method Calling
print('${dog.getName()} says ${dog.makeSound()}'); // Output: Buddy says Bark
print('${cat.getName()} says ${cat.makeSound()}'); // Output: Whiskers says Meow
print('${cow.getName()} says ${cow.makeSound()}'); // Output: Bessie says Moo
}
ব্যাখ্যা:
- Parent Class (
Animal): এখানেAnimalক্লাসটি একটি সাধারণ অবজেক্ট তৈরি করে যা একটি নাম গ্রহণ করে এবং একটি সাধারণ শব্দ তৈরি করে। - Child Classes (
Dog,Cat,Cow): এখানেDog,Cat, এবংCowক্লাসগুলিAnimalক্লাস থেকে উত্তরাধিকার সূত্রে প্রাপ্ত। তারা তাদের নিজস্ব শব্দ তৈরি করেmakeSoundমেথডের মাধ্যমে। - অবজেক্ট তৈরি এবং মেথড কল: প্রতিটি Child Class এর একটি অবজেক্ট তৈরি করা হয়েছে এবং তাদের মেথডগুলি কল করা হয়েছে।
এভাবে, Parent Class এর থেকে একাধিক Child Class তৈরি করে বিভিন্ন ধরনের অবজেক্ট তৈরি করা যায়।
Read more