জাভায় final কিওয়ার্ড ব্যবহারঃ
জাভায় final কিওয়ার্ডটি বিভিন্ন contexts এ ব্যবহার করা হয়। জাভায় final কিওয়ার্ড ব্যবহারে সীমাবদ্ধ করে দেয় একজন ব্যবহারকারিকে । final হতে পারে।
১. Variable
২. Method
৩. Class
১. final Variable: একটি final variable একবারই assign করা হয়। যদি final variable টি একবার রেফারেন্স করে দেওয়া হয় তাহলে অন্য কোন object দিয়ে পুনরায় ঐ variable কে re-bound হতে পারে না।
উদাহরণ দেখা যাকঃ-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main{ | |
public static void main(String args[]){ | |
final int variable = 30; // final variable | |
variable=43; | |
} | |
} |
বিঃদ্রঃ c++ const variable এবং জাভা final variable এর মধ্যে পার্থক্য আছে। সি ++ const variable যখন ডিক্লার করা হয় তখনই assign করে দিতে হয়। কিন্তু জাভার ক্ষেত্রে final variable assign প্রথমে না করলে হবে এটার প্রয়োজন নাই। তবে পরবর্তীতে assign করা যায়। অব্যশই একবার assign করতে হবে।
নিচের উদাহরণটি লক্ষ্য করা যাকঃ-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main{ | |
public static void main(String args[]){ | |
final int variable; // final variable kyeword | |
variable=43; // assigning later final variable | |
System.out.println(variable); | |
} | |
} |
Final Mehod: একটি final method কে subclass দ্বারা overridden করতে পারা যায় না।
আমারা যদি subclass দ্বারা final method কে overridden করতে যায় সেক্ষেত্রে ভুল গুলো নিচের প্রোগ্রামে লক্ষ্য করি।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Base{ | |
public final void display(){ // final keyword use | |
System.out.println("Base:: display() called"); | |
} | |
} | |
class Derived extends Base{ | |
public void display(){ // error here | |
System.out.println("Derived:: display() called"); | |
} | |
} | |
class FinalMeDemo{ | |
public static void main(String args[]){ | |
Base b = new Derived(); | |
b.display(); | |
} | |
} |
Final class: final class কে সম্প্রাসারিত (inherited) করতে পারা যায় না।
নিচের প্রোগ্রামটি লক্ষ্য করিঃ-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
final class Base{ // final keyword use in class | |
public final void display(){ | |
System.out.println("Base:: display() called"); | |
} | |
} | |
class Derived extends Base{ | |
public void display(){ // error here | |
System.out.println("Derived:: display() called"); | |
} | |
} | |
class FinalMeDemo{ | |
public static void main(String args[]){ | |
Base b = new Derived(); | |
b.display(); | |
} | |
} |
লেখার মধ্যে কিছু ভুল হলে আমাকে comment করেন জানাবেন। এছাড়া উপরের আলোচিত টপিক্স আর উপর আরও তথ্য আমাকে শেয়ার করতে পারেন।
ভাল থাকুন অন্য কোন topics নিয়ে অন্য কোন সময় আলোচনা করা হবে।