Final Keyword
final keyword is used for method, variables and classes
variable is constant
class does not inherit
if we create a final as a variable, it becomes constant, we cannot change the value of final.
if we create any final method, we cannot override it.
if we create a final class, it cannot extends it or inherit.
if we create a final as a variable, it becomes constant, we cannot change the value of final.
Example:
class Test { public static void main(String[] args) { final int i=10; i=i+20; // error cannot assign a value to final variable i System.out.println(i); } }
if we create any final method, we cannot override it.
Example:
class Test { final void m1() { System.out.println(" I am in collage"); } } class Demo extends Test { void m1() //error m1 college cannot be override { System.out.println(" I am in home"); } public static void main(String[] args) { } }
if we create a final class, it cannot extends it or inherit.
Example:
final class Test { void m1() { System.out.println(" I am in collage"); } } class Demo extends Test { void m1() //error m1 college cannot be override { System.out.println(" I am in home"); } public static void main(String[] args) { } }