Final variables and methods in Java

final variables and methods( ):

All methods and variables can be overridden by default in subclass. If we wish to parent the sub classes from overriding the members of the super class, we can declare them as final using the keyword final as a modifier.

Eg:

final int size=100;

final void shows status( ) {------}

Making a method final ensures that the functionality defined in this method will never be altered in anyway. Similarly, the value of a final variable can never be changed. Final variables, behave like class variables and they do not take any space on individual objects of the class.

final classes:

Sometimes we may like to present a class being further sub classes for security reasons. A class that can not be sub classed is called final class. this is achieved in java using the keyword final as follows.

final class  a class{--------}

final class B class extends some class{-------}

Any attempt to inherit these classes will cause an error and the compiler will not allow it. Declaring a class final prevents any unwanted extensions to the class.

Related

PROGRAM FOR CALCULATING AREA OF A RECTANGLE AND CIRCLE USING INTERFACES

import java.lang.*; interface Area {               final static float pi=3.14F;          ...

PROGRAM TO CALCULATE TOT, AVG AN D GRADE USING MULTILEVEL INHERITENCE

import java.util.*; class Adm {              int rno;              String...

PROGRAM FOR ENTERING DIFFERENT OBJECTS INTO VECTOR

import java.util.*; class vect {             public static void main(String args[])          &...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item