Final keyword in Java
It's used in variables, methods, and classes
1) Final variables - can't be reassigned the value
2) Final Method - Can't be override
3) Final Class - Can't be extends
1) Final Variable
2) Final Method
3) Final Class
The best example is String
Happy coding...😉
1) Final variables - can't be reassigned the value
2) Final Method - Can't be override
3) Final Class - Can't be extends
1) Final Variable
public class FinalKeyTutorial {
final static int name = 2;
public static void main(String[] arg)
{
name = 3; //Compiler error
System.out.println(name);
}
}
2) Final Method
public class StaticKeyword {
public static void main(String[] args) {
B b = new B();
b.show();
}
}
class A {
final void show()
{
System.out.print("baseclasse");
}
}
class B extends A {
void show()
{
System.out.print("sub class");
}
}
3) Final Class
The best example is String
public class FinalKeyTutorial {
public static void main(String[] args) {
FinalEx f =new FinalEx();
f.a = 20;
}
}
final class FinalEx{
int a = 10;
}
Happy coding...😉
Comments
Post a Comment