/* related posts with thumb nails */

final keyword:

Final variables: Final variables cannot be altered at later stages in the program once it has been initialized. These are similar to constant variables in C and C++ declared with the keyword ‘const’. These are used to prevent incidental or accidental changes to the variables whose values are supposed to be constant throughout. For example, a variable like ‘pi=3.14’ should not be modified in the program at any stage. They keyword to declare such variables is final. The naming convention used to declare final variables is to write them in capital letters. Final variables behave like class variables and they do not take any space on individual objects of the class. All the variables declared in an interface are final by default.

Example: final int SIZE=30;

Final Methods: Final methods cannot be overridden. Making a method final ensures that the functionality defined in this method cannot be altered in any way in child classes. This is usually done to avoid different behavior of child class objects when they call a same method. The following example demonstrates this concept.

class one


public final void show()


System.out.println( “Parent Rules”);



class two extends one


public void show() { }

// invalid because the method “show()” cannot be overridden


Final classes: A class that cannot be inherited is called Final class. Sometimes, we may wish to prevent a class from having it to be derived. Any attempt to take inheritance from such a class causes error. In simple terms, a final class is the one which cannot have any subclasses.

final class one { }

class two extends one { } // invalid because the class “one” is final.

Related Topics:


Post a Comment