final class – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 20 Jun 2020 15:28:01 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg final class – Programmerbay https://programmerbay.com 32 32 What is the use of final keyword in Java https://programmerbay.com/what-are-the-uses-of-final-keyword-in-java/ https://programmerbay.com/what-are-the-uses-of-final-keyword-in-java/#respond Tue, 27 Aug 2019 12:05:29 +0000 https://www.programmerbay.com/?p=4832 What is a final keyword in Java?

A final keyword can have a different definition in a different context. If we pick all common aspects from every context, then a final keyword can be defined as a keyword whose association with a class, variable or method makes them immutable ( that cannot be modified).

use of final keyword
A final keyword can be defined as a keyword which:
– restrict changes in the content or data
– A method, variable and even a class can be declared as final

Here are the following usage of final keyword in Java:-

Final Keyword with Classes

Declaring a class as final means that class can no longer be extended or inherited by other classes. Putting it differently, a final class can’t have its subclass, extending it would lead to a compile-time error.

Basically, the concept of the final class is useful as it provides assurance that no other class can override the methods and access the data members of that class. It guarantees the content of a class would not be changed in any way.

Syntax:

 final class_name {

// attributes and behviour

}

Program:

final class A{
int y;
// constructor
A(){
y=10;
}
void display(){

System.out.println(" Value of y ="+y);

}
}public class Test // extends A, triggers an error
{

public static void main(String[] args) {
A obj = new A();
obj.display();
}

}

Output:

Value of y =10

Explanation:

In the above program, we demonstrated how we can create a final class, However, if we try to extend this class then an error would be triggered, saying “cannot inherit from final A”

Final Keyword with Methods

When a method is preceded by the final keyword then its implementation or say functionality becomes immutable. In other words, the method can’t be overridden by subclasses.

Syntax:

final return_type method_name()

{

//implementation

}

Program:

class A{
int y;
// constructor
A(){
y=10;
}
final void display(){

System.out.println(" Value of y ="+y);

}
}


public class Test extends A
{
//error would be occurred
/* void display(){

System.out.println(" Value of y ="+y);
}*/

public static void main(String[] args) {
A obj = new A();
obj.display();
}

}

Output:

Value of y =10

Explanation:

In the above program, we demonstrated how we can create a final method. However, if we try to override this method then an error would be triggered, saying “cannot be overridden, display() is final”

Final keyword with formal parameters

When argument passed in the formal parameter is followed with the final keyword it simply means actual value passed from the actual parameter is immutable in the argument received by the respective method or constructor.

Syntax:

return_type method_name( final return_type variable)

{

//body

}

Program:

class A{
void display(final int n)
{
int x=10;
System.out.println("value ="+(x+n));
}
}

public class Test 
{


public static void main(String[] args) {
A obj = new A();
obj.display(10);
}

}

Output:

value =20

Explanation:

In the above program, we demonstrated how we can create a final local variable. However, if we try to reassign a value to this variable then an error would be triggered, saying “final parameter cannot be reassigned”

Final keyword with Variables

As we know, the value of a variable can be initialized multiple times at multiple places in a program and also can be changed at runtime. To avoid this, the final keyword is used which creates a constant. It means the initial value assigned to a variable can’t be modified.

In other words, once a variable gets a value, it can never be altered. The variable associated with the final keyword should be in uppercase. When it comes to reference variable that holds reference of an object. if it is declared as final then it would no longer able to refer to any other reference or say the physical copy of that object.

Syntax:

final data_type variable_name =  value;

Program:

public class Test
{
public static void main(String[] args) {
final int Y=10;
System.out.println("Value of y = "+Y);
// Y=30; error would occur
}

}

Output:

Value of y = 10

Explanation

In the above program, we demonstrated how we can create a final variable. However, if we try to reassign a value to this variable then an error would be triggered, saying “cannot assign a value to final variable y”

]]>
https://programmerbay.com/what-are-the-uses-of-final-keyword-in-java/feed/ 0