The final keyword in Java is used to restrict modification of variables, methods, and classes. Final variables cannot be changed once initialized, final methods cannot be overridden, and final classes cannot be subclassed. This keyword is useful for maintaining security and strict class order.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views5 pages
Final Keyword
The final keyword in Java is used to restrict modification of variables, methods, and classes. Final variables cannot be changed once initialized, final methods cannot be overridden, and final classes cannot be subclassed. This keyword is useful for maintaining security and strict class order.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5
TOPIC:- FINAL KEYWORD
WHY WE USE FINAL KEYWORD?
• In java, the final keyword is used to restrict the
modification of variables , methods & classes in various ways. FINAL VARIABLES:-
• When a variable is declared as final , than it’s value cannot be changed
once it is initialized. • We can say that the final keyword is works the same as a constant keyword. • Example:- • final int MAX_SPEED = 120; FINAL METHODS:-
• When a method is declared as final , it can be overridden by sub classes only.
• Example:- class Parent { public final void display(){ System.out.println("Display from Parent"); } } class Child extends Parent { // public void display() {
} } FINAL CLASSES:-
• When you declare a class as final, it cannot be subclassed.
• This is prevents other classes from inheriting from it, which can be useful for security or when you want to maintain a strict class order. • Example:- final class String { // ... String class implementation ... } // Trying to create a subclass of String will result in an error // class MyString extends String { ... }