Final variables:
- Any variable declare along with final modifier then those variables treated as final variable.
- if we declare final variables along with static will became constants.
- while declaring itself we need to assign these final variables.
- final int a;
class finalvariabledemo{
final int a=10;
public static void main(String args[]){
finalvariabledemo obj= new finalvariabledemo();
//obj.a=24; this will rise compile time error
}
}
Output:
Compile time Error.
Final Method:
- If you declare method as final that method also known as final methods.Final methods are not overridden.means we can't overridden that method in anyway.
- public final void add(){
}
public class A{
void add(){
//Can't override
}
}
Final Class:
- If you declare class is final that class is also known as final classes.Final classes are not extended.means we can't extends that class in anyway.
- public final class indhu{
}
public class classNotAllowed extends indhu {...} //not allowed
No comments