04 July 2023

Explain about Final Keyword In Java - learngreen.net

 In Java, the final keyword is utilized to define variables, methods and classes  that cannot be changed after assigning  a value . 


When we define variable as final, the value of the variable could not be changed. This denotes that once we assign value to final variable, we cant assign it a different value.


When we declared method as final we can not override that method using any subclass. This is useful when we want to prevent the method to be overriden.


When we declared class as final in Java , we can not make it subclass and we cant extend that class as well. This is useful when we didn't want class to be modified and extended as well.


Explain about Final Keyword In Java




In the above code we initialize data type int  with constant as 8 and and in next step if we try to change the value of constant variable we do get an error because we use Final keyword. 





In the above code we do create method message
and in next step we do create sub class with name X which extends superclass Final. If we try to override method message we do get compilation error as we used Final keyword their to prevent
method overriding.







In the above code we do create class with name final but before class word we use final keyword  &  if we try to extend class sub with superclass we do get an compilation error as we are using final keyword to create class with name final.


















No comments:

Post a Comment