কম্পিউটার

জাভাতে মেথড ওভাররাইডিং প্রতিরোধ করার কয়টি উপায়?


পদ্ধতি ওভাররাইডিং৷ জাভাতে রান-টাইম মেথড বাইন্ডিং বৈশিষ্ট্যের কারণে কাজ করে। সুতরাং, যদি আমরা জাভা কম্পাইলারকে একটি পদ্ধতির জন্য স্ট্যাটিক বাইন্ডিং করতে বাধ্য করি তাহলে আমরা সেই পদ্ধতিটিকে একটি প্রাপ্ত ক্লাসে ওভাররাইড হওয়া থেকে আটকাতে পারি।

আমরা 3 উপায়ে জাভাতে মেথড ওভাররাইডিং প্রতিরোধ করতে পারি

  • বেস ক্লাসে পদ্ধতি চূড়ান্ত করে
  • বেস ক্লাসে একটি মেথড স্ট্যাটিক করে
  • বেস ক্লাসে একটি পদ্ধতিকে প্রাইভেট করে

চূড়ান্ত পদ্ধতি ওভাররাইড করা যাবে না

একটি পদ্ধতি চূড়ান্ত করার মাধ্যমে আমরা একটি সীমাবদ্ধতা যোগ করছি যে প্রাপ্ত শ্রেণী এই নির্দিষ্ট পদ্ধতিকে ওভাররাইড করতে পারে না৷

উদাহরণ

class Base {
   public void show() {
      System.out.println("Base class show() method");
   }
   public final void test() {
      System.out.println("Base class test() method");
   }  
}
class Derived extends Base {
   public void show() {
      System.out.println("Derived class show() method");
   }
   // can not override test() method because its final in Base class
   /*
   *  public void test() { System.out.println("Derived class test() method"); }
   */
}
public class Test {
   public static void main(String[] args) {
      Base ref = new Derived();
   // Calling the final method test()
      ref.test();
   // Calling the overridden method show()
      ref.show();
   }
}

আউটপুট

Base class test() method
Derived class show() method

স্ট্যাটিক পদ্ধতি ওভাররাইড করা যাবে না

আমরা প্রাপ্ত ক্লাসে স্ট্যাটিক পদ্ধতিগুলিকে ওভাররাইড করতে পারি না কারণ স্ট্যাটিক পদ্ধতিগুলি বস্তুর সাথে নয়, ক্লাসের সাথে সংযুক্ত থাকে। এর মানে যখন আমরা একটি স্ট্যাটিক পদ্ধতি কল করি তখন JVM এটিতে এই রেফারেন্সটি পাস করে না যেমন এটি সমস্ত নন-স্ট্যাটিক পদ্ধতির জন্য করে। তাই স্ট্যাটিক পদ্ধতির জন্য রান-টাইম বাইন্ডিং হতে পারে না।

উদাহরণ

class Base {
   public void show() {
      System.out.println("Base class show() method");
   }
   public static void test() {
      System.out.println("Base class test() method");
   }
}
class Derived extends Base {
   public void show() {
      System.out.println("Derived class show() method");
   }
      // This is not an overridden method, this will be considered as new method in Derived class
   public static void test() {
      System.out.println("Derived class test() method");
   }
}
public class Test {
   public static void main(String[] args) {
      Base ref = new Derived();
      // It will call the Base class's test() because it had static binding
      ref.test();
      // Calling the overridden method show()
      ref.show();
   }
}

আউটপুট

Base class test() method
Derived class show() method

ব্যক্তিগত পদ্ধতি ওভাররাইড করা যাবে না

বেস ক্লাসের ব্যক্তিগত পদ্ধতিগুলি একটি প্রাপ্ত ক্লাসে দৃশ্যমান নয়, তাই সেগুলি ওভাররাইড করা যাবে না৷

উদাহরণ

class Base {
   public void show() {
      System.out.println("Base class show() method");
   }
   private void test() {
      System.out.println("Base class test() method");
   }
}
class Derived extends Base {
   public void show() {
      System.out.println("Derived class show() method");
   }
   // This is not an overridden method, this will be considered as other method.
   public void test() {
      System.out.println("Derived class test() method");
   }
}
public class Test {
   public static void main(String[] args) {
      Base ref = new Derived();
   // Cannot call the private method test(), this line will give compile time error
   // ref.test();
   // Calling the overridden method show()
      ref.show();
   }
}

আউটপুট

Derived class show() method

  1. কীভাবে জাভা ফাইল ক্লাস ব্যবহার করবেন

  2. কীভাবে জাভা পদ্ধতি ব্যবহার করবেন

  3. জাভা হ্যাশম্যাপ ক্লাস কিভাবে ব্যবহার করবেন

  4. জাভাতে ডিফল্ট পদ্ধতি ব্যবহার করে ডায়মন্ড সমস্যা কীভাবে সমাধান করবেন