Five Java programs demonstrating the use of the final keyword in Java
:Below are five Java programs demonstrating the use of the final keyword, along with step-by-step explanations for each.
Program-1: Final Variable
java public class FinalVariableExample { public static void main(String[] args) { final int MAX_VALUE = 100; System.out.println("Max Value: " + MAX_VALUE); // Uncommenting the following line will cause a compilation error // MAX_VALUE = 200; // Cannot assign a value to a final variable } }
Explanation :
1. Declaration: The variable MAX_VALUE is declared as final, which means it cannot be changed after its initial assignment.
2. Output: The program prints the value of MAX_VALUE, which is 100.
3. Error Handling: Attempting to reassign MAX_VALUE will result in a compilation error, demonstrating that `final` prevents modification.
Program-2: Final Method
java class Parent { final void display() { System.out.println("Final Method in java!!!!"); } } class Child extends Parent { // Uncommenting the following method will cause a compilation error // void display() { // System.out.println("Trying to override final method."); // } } public class FinalMethodExample { public static void main(String[] args) { Child child = new Child(); child.display(); // Calls the final method from Parent } }
Explanation
1. Final Method: The method display in the Parent class is declared as final, meaning it cannot be overridden in any subclass.
2. Child Class: The Child class attempts to override the display method. This is commented out, as it would lead to a compilation error if uncommented.
3. Execution: The main java method creates an instance of Child and calls display, which outputs the message from the `Parent` class.
Program-3: Final Class
java final class FinalClass { void show() { System.out.println("This is a final class."); } } // Uncommenting the following class will cause a compilation error // class SubClass extends FinalClass { // } public class FinalClassExample { public static void main(String[] args) { FinalClass obj = new FinalClass(); obj.show(); // Calls the method in the final class } }
Explanation
1. Final Class: The FinalClass is declared with the final keyword, preventing it from being subclassed.
2. SubClass Attempt: Any attempt to create a subclass from FinalClass will result in a compilation error (commented out in the code).
3. Method Call: The main method creates an instance of FinalClass and calls its show method.
Program 4: Final Parameter
java public class FinalParameterExample { void calculate(final int number) { // Uncommenting the following line will cause a compilation error // number = number + 10; // Cannot modify final parameter System.out.println("The number is: " + number); } public static void main(String[] args) { FinalParameterExample example = new FinalParameterExample(); example.calculate(20); // Passing a final parameter } }
Explanation
1. Final Parameter: The method calculate takes a parameter number declared as final, which means it cannot be modified within the method.
2. Error Handling: Attempting to change the value of number inside the method results in a compilation error (commented out).
3. Output: The main method calls calculate with 20, which is printed without modification.
Program 5: Final with Constructors
java class FinalField { final int value; // Constructor to initialize final field FinalField(int value) { this.value = value; // Assigning the value to the final field } void display() { System.out.println("Final Field Value: " + value); } } public class FinalFieldExample { public static void main(String[] args) { FinalField obj = new FinalField(50); obj.display(); // Displays the value of the final field } }
Explanation
1. Final Field: The class FinalField has a final field value which must be initialized.
2. Constructor: The constructor initializes value when an object of FinalField is created.
3. Display Method: The display method prints the value of the final field.
4. Execution: In main, an instance of FinalField is created with a value of 50, which is then displayed.