Final Classes, Final Variable and Final Method in Java
Final Class in Java
In Java programming language we use keyword final before class name definition. If you use final keyword to the class then that class can not have subclasses.Example of Final Class in Java
final class JavaTalk { } class BlogJavaTalk extends JavaTalk // This is give compilation error as //we cannot inherit from final class { }
Final Methods in Java
You can use keyword final with the methods. The final method can not be overridden. If method is implement with final keyword then you should not changed.
Example of Final Method in Java
class JavaTalk { public final String finalJava() { //Do something you want } } class OverrideJavaTalk extends JavaTalk { public final String finalJava() // This will throw error //as final method cannot be override { //Again Do something here } }
Final Variables
You can use keyword final with variables. If you can use variables with final then that variables make constant. If you can try to change final variables that gives compiler error.
As Example:
public class TestFinalVariable { private static final int x=2; //You can't reassign any value to x after this. code lile x=1 will throw error }
0 comments:
Post a Comment