Final Keyword in Java Demonstration
Final Keyword in Java Demonstration
Without final :
import [Link].*;
class one
{ int a=10;
void A()
{ [Link]("\n*****Welcome***");
{ void A()
{ a=20;
[Link]("\nBye");
[Link](+a);
class p27
b.A();
Output :
Using final keyword :
import [Link].*;
{ [Link]("\n*****Welcome***");
{ void A()
{ a=20;
[Link]("\nBye");
[Link](+a);
class p27
b.A();
Output :
When using the 'final' keyword incorrectly, the primary compile-time errors include attempting to extend a 'final' class and trying to override a 'final' method. In the example, if class 'one' is declared as final, any subclass such as 'two' attempting to extend it would result in an error. Similarly, if a method within 'one' is final, trying to override it in 'two' would also cause an error .
The 'final' keyword should be used when you want to restrict inheritance or prevent methods from being overridden, ensuring the integrity of the class design. Classes that are utility or have complete implementations often use 'final'. Additionally, for constant values, declaring them as 'final' ensures they remain unchanged throughout the application .
Using the 'final' keyword enhances application security and integrity by preventing unintended modification of classes, methods, and variables. Since 'final' restricts inheritance and method overriding, it ensures that the predefined behavior of methods and the structure of classes cannot be altered by subclasses, maintaining the intended design and functionality across the application .
By using 'final' on a class or method, you ensure that the behavior defined is preserved and cannot be altered by subclass implementation. This particularly helps in maintaining business logic integrity and preventing subclasses from changing the method's prototype or altering behavior contrary to the original design rationale, thereby preserving expected functionality .
If you attempt to change a final variable in a subclass that inherits from a superclass, you will encounter a compile-time error. This is because final variables are constants, and once initialized, their values cannot be changed. Any attempt to reassign a value to a final variable like 'a' in the given example results in a compilation error .
Without using the 'final' keyword, classes can be extended and methods can be overridden. This allows for polymorphic behavior, where subclasses can provide specific implementations for methods defined in their superclass. In the program example, class 'two' extends 'one' and overrides method A(), changing its behavior and accessing the variable 'a' to assign a new value .
Without the 'final' keyword, classes, variables, and methods can be freely inherited and overridden. In the example, class 'two' successfully extends class 'one' and overrides method A(). However, when using the 'final' keyword, class extensions and method overrides result in errors because 'final' restricts inheritance and overriding. Thus, you will see error messages if 'two' tries to extend 'one' or override a method when 'final' is applied .
Using the 'final' keyword on a class in Java prevents other classes from inheriting from it. In the provided example, the class 'one' is declared with the final keyword, which means it cannot be extended by any other class. Hence, when class 'two' attempts to extend 'one', it will result in a compile-time error .
When a variable is declared as final, its value cannot be changed once it has been initialized. This means if 'a' is declared as final, any attempt to change its value will result in a compile-time error, as seen in the example where 'a=20;' results in an error. Similarly, when a method is declared as final, it cannot be overridden by subclasses, restricting the ability of subclasses to alter the behavior of the method .
Declaring a method as 'final' prevents it from being overridden in any subclass, thus restricting polymorphic behavior. Polymorphism allows a subclass to alter method behavior, providing implementations tailored to subclass features. By making a method 'final', this flexibility is removed, ensuring the original method implementation remains unchanged across subclass hierarchy .