Final variable and methods

Final variable and methods :

All methods and variables can be overridden by default in sub classes. If we wish to prevent the sub classes from over riding the members of the super class, we can declare them as final using the keyword final as a modifier.  for example :

Final int size = 100;

Final void showstatus(){….}

Making a function final assures that the functionality defined in this method shall never be altered in any way. Similarly the value of a final variable can never be changed. Final variables behave like a clas variables and they do no take any space individual objects of the class.

Final classes

A class that cannot be subclassed is called a final class.

For example :

Final class aclass{..}

Final class bclass extends someclass{..}

Any attempt to inherit these classes shall cause an error and the compiler will not allow it .

Declaring the class final prevents any unwanted extensions to the class. It also allows the compiler to perform some optimizations when a method a class is invoked.

Note : We cannot inherit a class if we declare it final .

Finalizer methods

A constructor method is used to initialize an object when it is declared. This process is known as initialization. Java run time is an automatic garbage collecting system. It automatically frees up the memory resources used up by the objects. But objects may hold some other none object resources . In order to free these resources we must use finalizer method.

Finalization is just opposite of initialization. This is similar to destructors in c plus plus. The finalizr method is simply finalize() and can be added to any class. Java class that method when ever it is about to reclaim the space for that object. The finalize method should explicitly define the task to be performed.

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control