Saturday, July 11, 2015

Explain "final" keyword

final is a keyword in java which is used as a modifier for variables, methods and classes.
Final variables cannot be reassigned values. Once given, the value of a final variable cannot be changed. For final instance variables, you may assign value then and there, or in the constructor once. For final local variables within methods, value must be assigned before using the variable. The value cannot be changed for final variable. Final and static when used together with variables, define a constant in java. static means single copy and final means value cannot change. - Show simple example.
Final methods cannot be overridden. The subclass cannot redefine the method if superclass declares the method to be final. – Show simple example.

Final classes cannot be extended. Subclasses cannot be created for a final class. So a final class is typically a leaf class in the class heirarcgy – Show simple example

No comments:

Post a Comment