The final keyword in java is used to restrict the user. The java final keyword can be used in many contexts. Final can be:
- variable
- method
- class
The final keyword can be
applied with the variables, 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.
I.Java final
variable
If you
make any variable as final, you cannot change the value of final variable (It
will be constant).
Example:
There
is a final variable limit, we are going to change the value of this variable,
but it can't be changed because final variable once assigned a value can never
be changed.
public class FinalConstant{
final int limit = 90;//final variable
void myMethod(){
limit=400;
}
public static void main(String args[]){
FinalConstant obj=new FinalConstant();
obj.myMethod();
}
}//end of class
Output:Compile Time Error
public class FinalConstant{
final int limit = 90;//final variable
void myMethod(){
limit=400;
}
public static void main(String args[]){
FinalConstant obj=new FinalConstant();
obj.myMethod();
}
}//end of class
Output:Compile Time Error
II. Java final
method
If you
make any method as final, you cannot override it.
public class Employee {
final void getSalary(){
System.out.println("From Employee class");
}
}
class RegularEmployee extends Employee {
final void getSalary(){
System.out.println("From RegularEmployee class");
}
public static void main(String[] args) {
RegularEmployee re = new RegularEmployee();
re.getSalary();
}
}
Output:Compile Time Error
III. Java final
class
If you
make any class as final, you cannot extend it.
final class MyEmployee {
}
public class FinalClass extends MyEmployee {
public static void main(String[] args) {
FinalMethod re = new FinalMethod();
re.getSalary();
}
}
Output:Compile Time Error
Q) Is final method inherited?
Ans)
Yes, final method is inherited but you cannot override it. For Example:
class Employee {
void getSalary(){
//final void getSalary(){
System.out.println("From Employee class");
}
}
class FinalMethod extends Employee {
public static void main(String[] args) {
FinalMethod re = new FinalMethod();
re.getSalary();
}
}
Output : From Employee class
class Employee {
void getSalary(){
//final void getSalary(){
System.out.println("From Employee class");
}
}
class FinalMethod extends Employee {
public static void main(String[] args) {
FinalMethod re = new FinalMethod();
re.getSalary();
}
}
Output : From Employee class
Q) What is blank or uninitialized final variable?
A final
variable that is not initialized at the time of declaration is known as blank
final variable.
If you
want to create a variable that is initialized at the time of creating object
and once initialized may not be changed, it is useful. For example PAN CARD
number of an employee.
It can
be initialized only in constructor.
Example of blank final variable
class Employee{
String name;
final String EMPID;
...
}
Q) Can we initialize blank final variable?
Yes,
but only in constructor. For example:
class Employee{
final int ID;//blank final variable
Employee (){
ID=70;
System.out.println(ID);
}
public static void main(String args[]){
new Employee();
} }
Output:70
static blank final variable
A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.
Example of static blank final variable
class Employee{
static final int ID;//static blank final variable
static{ ID=50;}
public static void main(String args[]){
System.out.println(Employee.ID);
}
}
Q) What is final parameter?
If you
declare any parameter as final, you cannot change the value of it.
class EmpFinalParameter{
int salary(final int n){
n=n+2;//can't be changed as n is final
}
public static void main(String args[]){
EmpFinalParameter emp=new EmpFinalParameter ();
emp.salary(5);
}
}
Output:Compile Time Error
Q) Can we declare a constructor final?
No,
because constructor is never inherited.
No comments:
Post a Comment