HomeLinuxWhat's the “ultimate” Key phrase in Java?

What’s the “ultimate” Key phrase in Java?


Whereas programming in Java, there could be cases the place the developer wants to limit from overriding some particular functionalities. For example, securing or encrypting the info or when there’s a have to retailer the identical worth, all the time. In such instances, the “ultimate” key phrase in Java allows the developer to make the info confidential.

This weblog will elaborate on the utilization of the “ultimate” key phrase in Java.

What’s the “ultimate” Key phrase in Java?

The “ultimate” key phrase in Java is utilized to limit the person from overwriting a worth. It really works such that if a variable or a operate is allotted as ultimate, its worth can’t be overwritten.

The Java “ultimate” key phrase could be utilized in lots of contexts:

Instance 1: Utilization of “ultimate” Key phrase With Variable

On this instance, the “ultimate” key phrase could be related to a variable and overwritten as effectively to investigate its utilization:

ultimate int rating = 35;

rating = 45;

System.out.println(rating);

Within the above code snippet:

  • Firstly, initialize the acknowledged integer worth and affiliate the “ultimate” key phrase with it to make its worth unchangeable.
  • Within the subsequent step, overwrite the integer with one other worth and show it.
  • This can end in displaying an error because the worth related to the mentioned key phrase can’t be overwritten.

Output

Within the above output, the encountered exception signifies that the worth related to the “ultimate” key phrase can’t be overwritten.

Instance 2: Utilization of “ultimate” Key phrase With Operate

On this illustration, the mentioned key phrase could be utilized with an gathered operate within the class:

classparent{
 public ultimate void out() {
System.out.println(“That is the default operate”);
}}
classchildextendsparent{
 public void out() {
System.out.println(“That is an overridden operate”);  
}}
baby obj = new baby();
obj.out();

Within the above traces of code:

  • Firstly, outline a father or mother class named “father or mother”.
  • Inside the class, outline a operate named “out()” related to the “ultimate” key phrase and show the acknowledged message.
  • After that, create a baby class named “baby” inheriting the father or mother class with the assistance of the “extends” key phrase.
  • On this class, declare the operate “out()” an identical to the inherited class operate displaying the given message.
  • In primary, create an object of the “baby” class and invoke the acknowledged an identical operate.

Output

The confronted limitation within the above output signifies that the an identical operate can’t be overridden.

Instance 3: Utilization of “ultimate” Key phrase With Class

On this specific instance, a category could be allotted as “ultimate” after which could be verified by inheriting it by its baby class:

finalclassparent{
public ultimate void out1() {
System.out.println(“That is the father or mother class”);
}}
classchildextendsparent{
public void out2() {
System.out.println(“This can be a baby class”);   
}}
baby obj = new baby();
obj.out1();

Apply the below-stated steps as given within the above traces of code:

  • Firstly, outline a category named “father or mother” related to the “ultimate” key phrase to chorus from being inherited.
  • Inside the class, outline the supplied operate and show the given message.
  • After that, initialize the kid class “baby” inheriting the father or mother class by way of the “extends” key phrase.
  • On this class, likewise declare a operate named “out2()” and print the acknowledged message in its(operate) definition.
  • Lastly, in primary, create an object of the kid class and invoke the father or mother class operate “out1()”.
  • This can log an error because the class allotted as ultimate can’t be inherited.

Output

On this output, it may be seen that an exception is thrown because the father or mother class operate cannot be invoked by the kid class.

Conclusion

The “ultimate” key phrase in Java is utilized to chorus the person from overwriting a worth. This key phrase could be related to a variable, operate or class, and so on. Upon modifying or inheriting (within the case of sophistication) its worth, it logs an error. This text mentioned the utilization of the ultimate key phrase in Java.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments