In this topic, you will learn about, the concept of final classes, final methods, and final variables?
Final class: It is created by using two methods.
- Using the final keyword: In java, we can use a final keyword before a class keyword to create a final class. The final class cannot inherit using the final keyword. we can specify the class scope for the current program only.so, a final class cannot declare within a package
syntax: final class <class name>
{
Data member;
Member function;
}
2. Using final member: In the java language class specify all member’s final types and also represent the same as the final class.
Syntax:
Class <class name>
{
Final data member;
Final member function;
}
Final variable(keyword): It creates a constant variable. It works the same as the const keyword.
It also creates common memory for all class object like a static variable but a final variable cannot be re-initialized or change and must be initialized only once.
Syntax:
Final <data type><variable>=value;
Eg:- final float PI=3.14f:
Final method: It only uses a final variable. It executes only once for an object and the final method create using the final keyword before the return type.
Syntax:
Final <return type><function name>(argument)
{
Statement;
Statement;
}