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

Method Overloading in Java

In java it  is  possible to create  methods that have  the same but  different  parameter lists  and  different definitions . This is called  method ...

Explain different levels of access modifier available in java for protection.

It is possible to inherit all the members of a class by a subclass using the keyword extends. However, it may be necessary in some situations to restrict the access to certain variabl...

Give a Overview of Java Language? (or) Types of programs created in Java

Java is a general purpose object oriented programming language. We can develop two types of java programs: Stand – alone applications Web applets ...

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