FINAL KEY WORD IN JAVA
FINAL keyword in java is used mainly three concepts
1.variables
2.methods
3.class
1.variables. if we use final keyword with variables then we assinged the value of variable once than we can not change another.
syntax: final int a;
example:
class sum
{
public static void main(String args[])
{
final int a=10;
void xyz()
{
int a=20; // change value variable
}
}
}
out put compile time error
2.method: if we use final keyword with method then it can not be over ride in sub class.
syntax: final void m1()
{
// statements;
}
example:
{
public static void main(String args[])
{
int a=10;
final xyz()
{
System.out.println("hello java learning point");
}
}
}
class Test extends Sample
{
void xy()
{
System.out.println("good morning");
}
Test ob=new Test();
ob.xyz();
}
output
compile time error
3. class : if we use final keyword with class then these class can not be extends in another class.
syntax: final class
{
//body of class
}
example:
{
public static void main(String args[])
{
int a=10;
final xyz()
{
System.out.println("hello java learning point");
}
}
}
class Test extends Sample
{
void xy()
{
System.out.println("good morning");
}
Test ob=new Test();
ob.xyz();
}
output compile time error