In Java, the final keyword is used to restrict changes. Once something is marked as final, it cannot be modified further. It can be applied in three main places: variables, methods, and classes.
1. Final Variable
A final variable acts like a constant — its value cannot be changed once assigned.
public class GRT{
final int price = 15000;
public void display(){
GRT grt = new GRT();
grt.price = 200;
System.out.println(grt.price);
}
}
Key point:
- You must assign value once
- After that, no reassignment allowed
2. Final Method
A final method cannot be overridden by subclasses.
class Parent {
final void show() {
System.out.println("This is final method");
}
}
class Child extends Parent {
// void show() { } ❌ Error: cannot override final method
}
Key point:
- Used when you want to prevent method overriding
3. Final Class
A final class cannot be extended (inherited).
final class A {
void display() {
System.out.println("Final class");
}
}
// class B extends A { } ❌ Error: cannot inherit final class
Key point:
- Used when you want to stop inheritance

Top comments (0)