Important Keywords: Final and Abstract:
Final:
'final' keyword in java is used to restrict the user. It is applied to 'class' 'method' 'variable'
a final class cannot be extended
a final method cannot be overridden
final fields, parameters, and local variables cannot change their value once set
* a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.
Example:
class FinalExample{
// blank final variable
static final int Request_Number;
static{
Request_Number=4532134;
}
public static void main(String args[]){
System.out.println(Example.Request_Number;);
}
}
* A final method can not be overridden in sub-class. You should make a method final in java if you think it’s complete and its behavior should remain constant in sub-classes. Final methods are faster than non-final methods because they are not required to be resolved during run-time and they are bonded on compile time.
class Bird{
final void fly(){System.out.println("flying");}
}
class Eagle extends Bike{
// void fly(){System.out.println("flying High");} // will show compile time //error due to this
public static void main(String args[]){
Eagle egl= new Eagle();
egl.fly();
}
}
* If you make any class as final, you cannot extend it.
* Several classes in Java are final e.g. String, Integer and other wrapper classes
final class XYZ{
}
class ABC extends XYZ{ // will generate compile time error
void demo(){
System.out.println("ClassABC");
}
Here are few benefits or advantage of using final keyword in Java:
1. It improves performance. Not just JVM can cache final variable but also application can cache frequently use final variables.
2. Final variables are safe to share in multi-threading environment without additional synchronization overhead.
3. Final keyword allows JVM to optimized method, variable or class.
Final and Immutable Class in Java:
Final keyword helps to write immutable class. Immutable classes are the one which can not be modified once it gets created and String is primary example of immutable and final class.
Immutable classes offer several benefits one of them is that they are effectively read-only and can be safely shared in between multiple threads without any synchronization overhead.
You can not make a class immutable without making it final and hence final keyword is required to make a class immutable in java.
Important points on final in Java
1. Final keyword can be applied to member variable, local variable, method or class in Java.
2. Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.
3. You can not reassign value to final variable in Java.
4. Local final variable must be initializing during declaration.
5. Only final variable is accessible inside anonymous class in Java.
6. Final method can not be overridden in Java.
7. Final class can not be inherited in Java.
8. Final is different than finally keyword which is used on Exception handling in Java.
9. Final should not be confused with finalize() method which is declared in object class and called before an object is garbage collected by JVM.
10. All variable declared inside java interface are implicitly final.
11. Final and abstract are two opposite keyword and a final class can not be abstract in java.
12. Final methods are bonded during compile time also called static binding.
13. Final variables which is not initialized during declaration are called blank final variable and must be initialized on all constructor either explicitly or by calling this(). Failure to do so compiler will complain as "final variable (name) might not be initialized".
14. Making a class, method or variable final in Java helps to improve performance because JVM gets an opportunity to make assumption and optimization.
15. As per Java code convention final variables are treated as constant and written in all Caps e.g.
16. Making a collection reference variable final means only reference can not be changed but you can add, remove or change object inside collection. For example:
17. Final method is inherited but you cannot override it
18. There could be final parameter also, you cannot change the value of it.
abstract
The “abstract” keyword can be used on classes and methods. A class declared with the “abstract” keyword cannot be instantiated, and that is the only thing the “abstract” keyword does. Example of declaring a abstract class:
abstract Seasons (String name);
* Abstract class may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
* An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
Methods in an interface that are not declared as default or static are implicitly abstract, so the
A child class that inherits an abstract method must override it. If they do not, they must be abstract and any of their children must override it.
Final:
'final' keyword in java is used to restrict the user. It is applied to 'class' 'method' 'variable'
a final class cannot be extended
a final method cannot be overridden
final fields, parameters, and local variables cannot change their value once set
* a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.
Example:
class FinalExample{
// blank final variable
static final int Request_Number;
static{
Request_Number=4532134;
}
public static void main(String args[]){
System.out.println(Example.Request_Number;);
}
}
* A final method can not be overridden in sub-class. You should make a method final in java if you think it’s complete and its behavior should remain constant in sub-classes. Final methods are faster than non-final methods because they are not required to be resolved during run-time and they are bonded on compile time.
class Bird{
final void fly(){System.out.println("flying");}
}
class Eagle extends Bike{
// void fly(){System.out.println("flying High");} // will show compile time //error due to this
public static void main(String args[]){
Eagle egl= new Eagle();
egl.fly();
}
}
* If you make any class as final, you cannot extend it.
* Several classes in Java are final e.g. String, Integer and other wrapper classes
final class XYZ{
}
class ABC extends XYZ{ // will generate compile time error
void demo(){
System.out.println("ClassABC");
}
Here are few benefits or advantage of using final keyword in Java:
1. It improves performance. Not just JVM can cache final variable but also application can cache frequently use final variables.
2. Final variables are safe to share in multi-threading environment without additional synchronization overhead.
3. Final keyword allows JVM to optimized method, variable or class.
Final and Immutable Class in Java:
Final keyword helps to write immutable class. Immutable classes are the one which can not be modified once it gets created and String is primary example of immutable and final class.
Immutable classes offer several benefits one of them is that they are effectively read-only and can be safely shared in between multiple threads without any synchronization overhead.
You can not make a class immutable without making it final and hence final keyword is required to make a class immutable in java.
Important points on final in Java
1. Final keyword can be applied to member variable, local variable, method or class in Java.
2. Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.
3. You can not reassign value to final variable in Java.
4. Local final variable must be initializing during declaration.
5. Only final variable is accessible inside anonymous class in Java.
6. Final method can not be overridden in Java.
7. Final class can not be inherited in Java.
8. Final is different than finally keyword which is used on Exception handling in Java.
9. Final should not be confused with finalize() method which is declared in object class and called before an object is garbage collected by JVM.
10. All variable declared inside java interface are implicitly final.
11. Final and abstract are two opposite keyword and a final class can not be abstract in java.
12. Final methods are bonded during compile time also called static binding.
13. Final variables which is not initialized during declaration are called blank final variable and must be initialized on all constructor either explicitly or by calling this(). Failure to do so compiler will complain as "final variable (name) might not be initialized".
14. Making a class, method or variable final in Java helps to improve performance because JVM gets an opportunity to make assumption and optimization.
15. As per Java code convention final variables are treated as constant and written in all Caps e.g.
16. Making a collection reference variable final means only reference can not be changed but you can add, remove or change object inside collection. For example:
17. Final method is inherited but you cannot override it
18. There could be final parameter also, you cannot change the value of it.
abstract
The “abstract” keyword can be used on classes and methods. A class declared with the “abstract” keyword cannot be instantiated, and that is the only thing the “abstract” keyword does. Example of declaring a abstract class:
abstract Seasons (String name);
* Abstract class may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
* An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void abstractMethodExample(String parameter1);
Methods in an interface that are not declared as default or static are implicitly abstract, so the
abstract modifier is not used with interface methods.A child class that inherits an abstract method must override it. If they do not, they must be abstract and any of their children must override it.
Comments
Post a Comment