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:
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:
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:
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.