কম্পিউটার

আমরা কি জাভাতে স্ট্যাটিক পদ্ধতি ওভারলোড বা ওভাররাইড করতে পারি?


স্ট্যাটিক পদ্ধতি, জাভাতে ওভারলোড করা যেতে পারে। কিন্তু একটি শর্ত আছে যে দুটি পদ্ধতিকে ওভারলোড করা যাবে না I তারা শুধুমাত্র 'static' কীওয়ার্ডের কারণে আলাদা।

আসুন একটি উদাহরণ দেখি -

উদাহরণ

public class Demo{
   public static void test(){
      System.out.println("Demo class test function has been called");
   }
   public static void test(int val){
      System.out.println("Demo class test function with parameter has been called");
   }
   public static void main(String args[]){
      System.out.println("In the main class, Demo functions being called");
      Demo.test();
      Demo.test(57);
   }
}

আউটপুট

In the main class, Demo functions being called
Demo class test function has been called
Demo class test function with parameter has been called

ডেমো নামের একটি ক্লাসে 'টেস্ট' নামে একটি ফাংশন রয়েছে যা একটি নির্দিষ্ট বার্তা প্রিন্ট করে। এটি একটি প্যারামিটার হিসাবে একটি পূর্ণসংখ্যা মান সহ 'পরীক্ষা' নামে আরেকটি ফাংশনকে সংজ্ঞায়িত করে। প্রাসঙ্গিক বার্তা ফাংশন বডি ভিতরে দেখানো হয়. প্রধান ফাংশনে, টেস্ট ফাংশনটিকে প্যারামিটার ছাড়া এবং একটি পূর্ণসংখ্যা প্যারামিটার সহ বলা হয়। প্রাসঙ্গিক বার্তা কনসোলে প্রদর্শিত হয়৷

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

উদাহরণ

class base_class{
   public static void show(){
      System.out.println("Static or class method from the base class");
   }
   public void print_it(){
      System.out.println("Non-static or an instance method from the base class");
   }
}
class derived_class extends base_class{
   public static void show(){
      System.out.println("Static or class method from the derived class");
   }
   public void print_it(){
      System.out.println("Non-static or an instance method from the derived class");
   }
}
public class Demo{
   public static void main(String args[]){
      base_class my_instance = new derived_class();
      System.out.println("Base class instance created.");
      my_instance.show();
      System.out.println("Function show called");
      my_instance.print_it();
      System.out.println("Function print_it called");
   }
}

আউটপুট

Base class instance created.
Static or class method from the base class
Function show called
Non-static or an instance method from the derived class
Function print_it called

বেস ক্লাসে 'শো' নামে একটি স্ট্যাটিক ফাংশন রয়েছে যা একটি বার্তা প্রিন্ট করে। একইভাবে, 'print_it' নামের আরেকটি ফাংশন একটি বার্তা প্রদর্শন করে। একটি ক্লাস বেস ক্লাস থেকে উদ্ভূত হয় যা দুটি ফাংশন উত্তরাধিকারসূত্রে পায়। ডেমো নামের একটি ক্লাসে প্রধান ফাংশন রয়েছে যা বেস ক্লাসের একটি উদাহরণ তৈরি করে যা প্রাপ্ত ধরণের ক্লাসের। প্রাসঙ্গিক বার্তাগুলি কনসোলে প্রদর্শিত হয়৷


  1. Enum জাভা কোন ক্লাস প্রসারিত করতে পারেন?

  2. আমরা কি জাভাতে একটি সুরক্ষিত পদ্ধতি ওভাররাইড করতে পারি?

  3. কিভাবে আমরা জাভাতে একটি JTextField এ প্যাডিং যোগ করতে পারি?

  4. ইন্টারফেসে জাভা 8 স্ট্যাটিক পদ্ধতি