0% found this document useful (0 votes)
10 views

Final Modifier

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Final Modifier

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Final Modifier : Final modifier is used for restriction purpose.

Final modifier we can use with


1)Final with variable
2)final with Method
3)Final with class

1)Final with variable: If we use final with variable then we cant reassign the
value .

Example :

package finalModifier;

public class FinalWithVariable {

public static void main(String[] args) {

final int a = 20;


System.out.println(a);
a = 30;
System.out.println(a);
}
}

2) Final with method : if we use final with method then we cant override that
method into child class. (**********)
Example :

package finalModifier;

public class Parent {

public final void m1() {

System.out.println("m1 method of Parent class");


}
}

package finalModifier;

public class Child extends Parent {

public void m1() {


System.out.println("M1 method of Child class");
}

public static void main(String[] args) {

Child c = new Child();


c.m1();
}
}
3)Final with class: If we declare class as final then we cant extend that class
into child class.

Example :

package finalModifier;

public final class Parent {

package finalModifier;

public class Child extends Parent {

public static void main(String[] args) {

}
}

You might also like