Final - Abstract Keywords in Java
Final - Abstract Keywords in Java
In Java, the final keyword can be used while declaring an entity. Using the final keyword means that the value can't be modified in the
future. This entity can be - but is not limited to - a variable, a class or a method.
A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is
called a "blank final" variable. A blank final instance variable of a class must be definitely assigned in every constructor of the class in which it is declared; similarly,
a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases
Final Methods
Methods marked as final cannot be overridden. When we design a class and feel that a method shouldn’t be overridden, we can make this method final.
We can also find many final methods in Java core libraries.
Final Class
If the class is final, we can’t extend it to override the method and fix the problem. In other words, we lose extensibility, one of the
benefits of object-oriented programming.
Abstract classes cannot be instantiated due to their partial implementation, but they can be inherited just like a normal class.
When an abstract class is inherited, the subclass usually provides implementations for all of the abstract methods in its parent class. However,
if it does not, then the subclass must also be declared abstract.
abstract class syntax
public abstract class DemoClass
{
//declare other methods and fields
}