Subtyping এবং Inheritance কি এবং Generics এ এর ব্যবহার

Generics এর মাধ্যমে Subtyping এবং Inheritance - জাভা জেনেরিক্স (Java Generics) - Java Technologies

317

জাভাতে Subtyping এবং Inheritance হল মূল ভিত্তি, যা জেনেরিক্স ব্যবহার করার সময়ও গুরুত্বপূর্ণ। এই দুটি ধারণা জেনেরিক ক্লাস, মেথড, এবং ডেটা টাইপের মধ্যে টাইপ সম্পর্ক স্থাপন এবং টাইপ সেফটি নিশ্চিত করতে সহায়তা করে।


Subtyping এবং Inheritance কি?

  1. Subtyping:
    • Subtyping একটি টাইপ সিস্টেমের মধ্যে সম্পর্ক স্থাপন করে, যেখানে একটি টাইপ অন্য টাইপের subtype হয়।
    • উদাহরণ:
      • Integer হল Number এর একটি subtype।
      • ArrayList<Integer> হল List<Integer> এর একটি subtype।
  2. Inheritance:
    • Inheritance হল ক্লাসের মধ্যে সম্পর্ক যেখানে একটি ক্লাস অন্য ক্লাসের বৈশিষ্ট্য এবং আচরণ উত্তরাধিকার সূত্রে পায়।
    • উদাহরণ:

      class Animal {}
      class Dog extends Animal {}
      

Generics এ Subtyping এবং Inheritance এর ভূমিকা

জেনেরিক্স ব্যবহার করার সময়, Subtyping এবং Inheritance কিছু নিয়ম অনুযায়ী কাজ করে।

Generics এবং Subtyping এর উদাহরণ

সাধারণ উদাহরণ:

List<Integer> intList = new ArrayList<>();
List<Number> numList = new ArrayList<>();
// numList = intList; // Compile-time error: incompatible types

নিয়ম:

  • List<Integer> এবং List<Number> একে অপরের subtype নয়, যদিও Integer হল Number এর subtype।

Wildcards দিয়ে Subtyping:

Wildcards (?) ব্যবহার করে Subtyping সম্পর্ক স্থাপন করা যায়।

List<? extends Number> numList = new ArrayList<Integer>();
List<? super Integer> intList = new ArrayList<Number>();

Generics এবং Inheritance এর উদাহরণ

Generics এবং Inheritance:

class Animal {}
class Dog extends Animal {}

class GenericClass<T> {}

public class Main {
    public static void main(String[] args) {
        GenericClass<Animal> animalObj = new GenericClass<>();
        GenericClass<Dog> dogObj = new GenericClass<>();
        // animalObj = dogObj; // Compile-time error
    }
}

কারণ: GenericClass<Dog> এবং GenericClass<Animal> একে অপরের subtype নয়, যদিও Dog হল Animal এর subclass।


Wildcards দিয়ে Subtyping এবং Inheritance

  1. Upper Bounded Wildcards (<? extends T>):

    • টাইপ প্যারামিটার T বা তার সাবটাইপগুলিকে মেনে চলে।
    public static void printAnimals(List<? extends Animal> animals) {
        for (Animal animal : animals) {
            System.out.println(animal);
        }
    }
    
    public static void main(String[] args) {
        List<Dog> dogs = new ArrayList<>();
        printAnimals(dogs); // Works
    }
    
  2. Lower Bounded Wildcards (<? super T>):

    • টাইপ প্যারামিটার T বা তার সুপারটাইপগুলিকে মেনে চলে।
    public static void addDogs(List<? super Dog> animals) {
        animals.add(new Dog());
    }
    
    public static void main(String[] args) {
        List<Animal> animals = new ArrayList<>();
        addDogs(animals); // Works
    }
    

Generics, Subtyping এবং Polymorphism

Generics এর মাধ্যমে Subtyping এবং Polymorphism একত্রে ব্যবহারের সুবিধা পাওয়া যায়।

উদাহরণ:

class Shape {}
class Circle extends Shape {}

public class Main {
    public static void drawShapes(List<? extends Shape> shapes) {
        for (Shape shape : shapes) {
            System.out.println("Drawing shape: " + shape);
        }
    }

    public static void main(String[] args) {
        List<Circle> circles = new ArrayList<>();
        drawShapes(circles); // Works because of `<? extends Shape>`
    }
}

Generics এ Subtyping এবং Inheritance এর ব্যবহার: Advantages

  1. টাইপ সেফটি: Subtyping এবং Inheritance টাইপ সেফ কোড নিশ্চিত করে।
  2. কোড পুনঃব্যবহার: Wildcards দিয়ে বিভিন্ন টাইপের জন্য একই জেনেরিক কোড ব্যবহার করা যায়।
  3. Flexibility: Generics এর মাধ্যমে Subtyping ব্যবহার করে Polymorphic কোড তৈরি করা যায়।
  4. Compile-Time Checking: টাইপ সম্পর্কিত ত্রুটি কম্পাইল-টাইমে ধরা পড়ে, Runtime ত্রুটি কমায়।

Generics এ Subtyping এবং Inheritance ব্যবহার করার মাধ্যমে জাভায় টাইপ সেফ এবং ফ্লেক্সিবল কোড তৈরি করা যায়। Wildcards (<? extends T>, <? super T>) এই দুটি ধারণার কার্যকারিতা বাড়ায় এবং জেনেরিক ক্লাস বা মেথডগুলোর পুনঃব্যবহারযোগ্যতা বাড়ায়।

Content added By
Promotion

Are you sure to start over?

Loading...