Final Keyword
The final keyword in java is basically used to make a variable as constant and restrict the user also.Where to apply final keyword in java
Java final keyword is very important keyword and can be applied to :
If your make any class as final, you cannot inherit or extends it by other classes. When we want to restrict inheritance then we make a class as final.
For example:
For example:
final class Animal
{
void typesOfAnimal()
{
System.out.println("there are different types of animal on the earth");
}
}
class Dog extends Animal
{
public static void main(String args[])
{
Dog d = new Dog();
d.typesOfAnimal();
}
}
output: compile time error
but in the below example , if you apply final with child class , you can easily inherit or extends parent class there is no restriction.
For example:
class Animal
{
void typesOfAnimal()
{
System.out.println("there is different types of animal on the earth");
}
}
final class Dog extends Animal
{
void run()
{
System.out.println("dog run fast");
}
public static void main(String args[])
{
Dog d = new Dog();
d.typesOfAnimal();
d.run();
}
}
output: there is different types of animal on the earth
dog run fast
but in the below example , if you apply final with child class , you can easily inherit or extends parent class there is no restriction.
For example:
class Animal
{
void typesOfAnimal()
{
System.out.println("there is different types of animal on the earth");
}
}
final class Dog extends Animal
{
void run()
{
System.out.println("dog run fast");
}
public static void main(String args[])
{
Dog d = new Dog();
d.typesOfAnimal();
d.run();
}
}
output: there is different types of animal on the earth
dog run fast
(2) Java final variable
Final keyword in java is used to make a variable as constant. If you make any variable as final variable, You can't change the value of final variable.
For example:
There is a final variable speed and now we are going to change the value of this variable, but it cannot be changed because final variable once assigned a value can never be changed.
class Bus
{
final int speed = 70;//final variable
void change()
{
speed = 50;
}
public static void main(String args[])
{
Bus b = new Bus();
b.change();
}
}
output: compile time error
(3) Java final method
If you make any method as final, you cannot override this method i.e child class cannot override parent class method, if parent class method declared as final.
For example:
class Parent
{
final void salary()
{
System.out.println("parent's salary is 5000");
}
}
class Child extends Parent
{
void salary()
{
System.out.println("child's salary is 6000");
}
public static void main(String args[])
{
Child c =new Child();
Child c =new Child();
c.salary();
}
}
output: compile time error
output: compile time error
Blank or Uninitialized final variable
A final variable that is not initialized at the time of declaration is known as blank or uninitialized final variable.
We must initialize the blank final variable in a constructor of the class otherwise it will throw a compilation error.
For example:
class Student
{
final int rollNo;//blank final variable
Student()
{
rollNo = 64;
}
void show()
{
System.out.println(rollNo);
}
public static void main(String args[])
{
Student s = new Student();
s.show();
}
}
output: 64
static blank final variable
static blank final variable is variable which is not initialized at the time of declaration is known as static blank final variable.
static blank final variable can be initialized only in static block.
For example:
class Employee
{
static final int id;//static blank final variable
static
{
id = 10;
System.out.println(id);
}
public static void main(String args[])
{
System.out.println("main method");
}
}
output: 10
main method
Q. Can we declare constructor as final ?
Ans. No, constructor cannot be declared as final.
Q. Can we inherit final method ?
Ans. Yes, we can inherit final method, but cannot override final method.
For example:
class Parent
{
final void service()
{
System.out.println("parent is a English teacher");
}
}
class Child extends Parent
{
public static void main(String args[])
{
Child c = new Child();
c.service();
}
}
output: parent is a English teacher
Q. Can we declare constructor as final ?
Ans. No, constructor cannot be declared as final.
Q. Can we inherit final method ?
Ans. Yes, we can inherit final method, but cannot override final method.
For example:
class Parent
{
final void service()
{
System.out.println("parent is a English teacher");
}
}
class Child extends Parent
{
public static void main(String args[])
{
Child c = new Child();
c.service();
}
}
output: parent is a English teacher
final Parameter
If you apply final keyword with any parameter, you can't change the value of it.
For example:
class Demo
{
int cube(final int a)
{
a = a + 5;//cannot be changed because a is final
a*a*a;
}
public static void main(String args[])
{
Demo d = new Demo();
d.cube(6);
}
0 comments:
Post a Comment